|
| 1 | +var GestureViewController = JSB.defineClass('GestureViewController : UIViewController', { |
| 2 | + viewDidLoad: function() { |
| 3 | + self.navigationItem.title = 'Gesture'; |
| 4 | + |
| 5 | + var swipeView = UIView.alloc().initWithFrame({x: 10, y: 300, width: 300, height: 120}); |
| 6 | + swipeView.backgroundColor = UIColor.greenColor(); |
| 7 | + |
| 8 | + var swipeLabel = UILabel.alloc().initWithFrame(swipeView.bounds); |
| 9 | + swipeLabel.text = 'Swipe'; |
| 10 | + swipeLabel.textAlignment = 1; |
| 11 | + swipeLabel.textColor = UIColor.whiteColor(); |
| 12 | + swipeView.addSubview(swipeLabel); |
| 13 | + |
| 14 | + self.view.addSubview(swipeView); |
| 15 | + |
| 16 | + var swipeGestureRecognizer = UISwipeGestureRecognizer.alloc().initWithTargetAction(self, 'handleSwipe:'); |
| 17 | + swipeGestureRecognizer.direction = 1 << 0 | 1 << 1; |
| 18 | + swipeView.addGestureRecognizer(swipeGestureRecognizer); |
| 19 | + |
| 20 | + var tapView = UIView.alloc().initWithFrame({x: 80, y: 100, width: 80, height: 80}); |
| 21 | + tapView.backgroundColor = UIColor.redColor(); |
| 22 | + |
| 23 | + var tapLabel = UILabel.alloc().initWithFrame(tapView.bounds); |
| 24 | + tapLabel.text = 'Tap'; |
| 25 | + tapLabel.textAlignment = 1; |
| 26 | + tapLabel.textColor = UIColor.whiteColor(); |
| 27 | + tapView.addSubview(tapLabel); |
| 28 | + |
| 29 | + self.view.addSubview(tapView); |
| 30 | + |
| 31 | + var tapGestureRecognizer = UITapGestureRecognizer.alloc().initWithTargetAction(self, 'handleTap:'); |
| 32 | + tapView.addGestureRecognizer(tapGestureRecognizer); |
| 33 | + |
| 34 | + var panView = UIView.alloc().initWithFrame({x: 180, y: 200, width: 80, height: 80}); |
| 35 | + panView.backgroundColor = UIColor.blueColor(); |
| 36 | + |
| 37 | + var panLabel = UILabel.alloc().initWithFrame(panView.bounds); |
| 38 | + panLabel.text = 'Pan'; |
| 39 | + panLabel.textAlignment = 1; |
| 40 | + panLabel.textColor = UIColor.whiteColor(); |
| 41 | + panView.addSubview(panLabel); |
| 42 | + |
| 43 | + self.view.addSubview(panView); |
| 44 | + |
| 45 | + var panGestureRecognizer = UIPanGestureRecognizer.alloc().initWithTargetAction(self, 'handlePan:'); |
| 46 | + panView.addGestureRecognizer(panGestureRecognizer); |
| 47 | + }, |
| 48 | + handleTap: function(sender) { |
| 49 | + var alertView = UIAlertView.new(); |
| 50 | + alertView.message = 'Tapped.'; |
| 51 | + alertView.addButtonWithTitle('OK'); |
| 52 | + alertView.show(); |
| 53 | + }, |
| 54 | + handlePan: function(sender) { |
| 55 | + var point = sender.translationInView(sender.view); |
| 56 | + var center = sender.view.center; |
| 57 | + sender.view.center = {x: center.x + point.x, y: center.y + point.y}; |
| 58 | + |
| 59 | + sender.setTranslationInView({x: 0, y: 0}, sender.view); |
| 60 | + }, |
| 61 | + handleSwipe: function(sender) { |
| 62 | + var label = UILabel.alloc().initWithFrame({x: 0, y: 0, width: 180, height: 60}); |
| 63 | + label.backgroundColor = UIColor.colorWithWhiteAlpha(0.0, 0.5); |
| 64 | + label.center = self.view.center; |
| 65 | + label.alpha = 0.0; |
| 66 | + label.text = 'Swiped'; |
| 67 | + label.textAlignment = 1; |
| 68 | + label.textColor = UIColor.whiteColor(); |
| 69 | + self.view.addSubview(label); |
| 70 | + |
| 71 | + UIView.animateWithDurationDelayOptionsAnimationsCompletion(0.3, 0.0, 0 << 16, function() { |
| 72 | + label.alpha = 1.0; |
| 73 | + }, function(finished) { |
| 74 | + UIView.animateWithDurationDelayOptionsAnimationsCompletion(0.3, 0.5, 0 << 16, function() { |
| 75 | + label.alpha = 0.0; |
| 76 | + }, function(finished) { |
| 77 | + label.removeFromSuperView(); |
| 78 | + }); |
| 79 | + }); |
| 80 | + } |
| 81 | +}); |
| 82 | + |
| 83 | +JSB.exports = GestureViewController; |
0 commit comments