多點觸控與手勢
UIResponder - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event touches : 觸控事件的集合. event: An object representing the event to which the touches belong. 當有一個或多個手指觸碰到view or window時會執行 此方法 多點觸控預設是關閉的,需將view的 multipleTouchEnabled 屬性設定為Yes.
UIResponder - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
UITouch - (CGPoint)locationInView:(UIView *)view Property 設定坐標系 若為nil,是使用 window’s coordinates Property NSUInteger tapCount Tap的次數
按Option(or Alt)鍵可以模擬2點觸控 (void)viewDidLoad { [super viewDidLoad]; self.view.multipleTouchEnabled=YES; } -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ int i=0; for (UITouch *t in touches){ i++; CGPoint point=[t locationInView:self.view]; NSLog(@"touchesBegan: %d tapCount:%d x:%f y:%f",i,t.tapCount,point.x,point.y); -(void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{ NSLog(@"touchesCancelled"); -(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ NSLog(@"touchesEnded"); -(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ NSLog(@"touchesMoved"); 按Option(or Alt)鍵可以模擬2點觸控
練習 建立一view-based專案,在畫面中放入一 ImageView,手指點到圖片並移動時,圖片會跟 著移動 Touch point center
int x,y; -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ UITouch *touch=[touches anyObject]; CGPoint point=[touch locationInView:[self superview]]; x=self.center.x-point.x; y=self.center.y-point.y; } -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ CGPoint p=CGPointMake(point.x+x, point.y+y); [self setCenter:p];
手勢(Gesture) UIGestureRecognizer abstract base class The concrete subclasses of UIGestureRecognizer are the following: UITapGestureRecognizer UIPinchGestureRecognizer UIRotationGestureRecognizer UISwipeGestureRecognizer UIPanGestureRecognizer UILongPressGestureRecognizer UIScreenEdgePanGestureRecognizer
Managing Gesture Recognizers UIView (void)addGestureRecognizer:(UIGestureRecognizer *) gestureRecognizer 在view 中加入手勢辨識 (void)removeGestureRecognizer: (UIGestureRecognizer *)gestureRecognizer 移除手勢辨識.
Managing Gesture Recognizers UIView @property(nonatomic,copy) NSArray * gestureRecognizers 回傳已加入view中的手勢
UIGestureRecognizer 初始化手勢辨識 (id)initWithTarget:(id)target action:(SEL)action 設定手勢動作發生時要執行的方法
UIGestureRecognizer - (CGPoint)locationInView:(UIView *)view 回傳座標點 通常是多個觸控的中心點 UITapGestureRecognizer 若有多個taps這個是回傳第一個tap的座標點 若有多個觸控點這個是回傳多個觸控的中心點 UISwipeGestureRecognizer 回傳手勢開始的座標點
UIGestureRecognizer - (CGPoint)locationOfTouch:(NSUInteger) touchIndex inView:(UIView *)view 回傳指定觸控點的座標 @property(nonatomic, readonly) UIView * view 回傳此手勢所加入的view
UITapGestureRecognizer Property NSUInteger numberOfTapsRequired 設定手勢taps的次數 NSUInteger numberOfTouchesRequired 設定手勢需多少手指觸碰
UISwipeGestureRecognizer Property UISwipeGestureRecognizerDirection direction 設定手勢方向 NSUInteger numberOfTouchesRequired 設定手勢需多少手指觸碰 typedef enum { UISwipeGestureRecognizerDirectionRight = 1 << 0, UISwipeGestureRecognizerDirectionLeft = 1 << 1, UISwipeGestureRecognizerDirectionUp = 1 << 2, UISwipeGestureRecognizerDirectionDown = 1 << 3 } UISwipeGestureRecognizerDirection;
(void)viewDidLoad { [super viewDidLoad]; UISwipeGestureRecognizer *swipeLeft=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeLeft:)]; swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft; [self.view addGestureRecognizer:swipeLeft]; UISwipeGestureRecognizer *swipeRight= [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeRight:)]; swipeRight.direction = UISwipeGestureRecognizerDirectionRight; [self.view addGestureRecognizer:swipeRight]; } (void)handleSwipeLeft:(UISwipeGestureRecognizer *)recognizer { CGPoint location = [recognizer locationInView:self.view]; NSLog(@"Swipe left started at (%f,%f)",location.x,location.y); (void)handleSwipeRight:(UISwipeGestureRecognizer *)recognizer { NSLog(@"Swipe right started at (%f,%f)",location.x,location.y);
UIRotationGestureRecognizer 旋轉手勢需2個手指觸碰 2個手指以反方向畫圓 Property CGFloat rotation 旋轉弧度 CGFloat velocity 旋轉弧度速度.
UIPinchGestureRecognizer Pinching gestures需2個手指觸碰 2個手指互相靠近:縮小 2個手指互相遠離:放大 屬性 CGFloat scale 放大縮小比例 >1: zoom-in <1:zoom-out CGFloat velocity 放大縮小速度
UIPanGestureRecognizer Panning (dragging) gestures拖拉手勢 Property NSUInteger maximumNumberOfTouches 設定最大手指頭數量 NSUInteger minimumNumberOfTouches 設定最小手指頭數量.
UIPanGestureRecognizer -(CGPoint)translationInView:(UIView *)view 移動距離 - (CGPoint)velocityInView:(UIView *)view 移動速度 - (void)setTranslation:(CGPoint)translation inView:(UIView *)view 設定移動值
練習 將UILabel用手勢拖動到任意位置
- (void)viewDidLoad { [super viewDidLoad]; UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 50)]; label.text = @"Drag me!"; label.userInteractionEnabled = YES; UIPanGestureRecognizer *gesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(labelDragged:)]; [label addGestureRecognizer:gesture]; [self.view addSubview:label]; } - (void)labelDragged:(UIPanGestureRecognizer *)gesture{ UILabel *label = (UILabel *)gesture.view; CGPoint translation = [gesture translationInView:label]; label.center = CGPointMake(label.center.x + translation.x, label.center.y + translation.y); [gesture setTranslation:CGPointZero inView:label];
UILongPressGestureRecognizer 需一個或多個手指觸碰指定時間 手指指可以移動指定內的距離 若移動超出設定距離,手勢辨識失敗
UILongPressGestureRecognizer Property CGFloat allowableMovement 設定最大可移動距離 default distance :10 point. CFTimeInterval minimumPressDuration 設定最小觸碰時間. default duration : 0.5 seconds.
UILongPressGestureRecognizer Property NSUInteger numberOfTapsRequired 設定tap次數 NSInteger numberOfTouchesRequired 設定觸碰手指個數
UIScreenEdgePanGestureRecognizer 和panning (dragging) 手勢類似,但從視窗邊緣 開始 Property UIRectEdge edges 設定手勢開始的邊緣 typedef enum : NSUInteger { UIRectEdgeNone = 0, UIRectEdgeTop = 1 << 0, UIRectEdgeLeft = 1 << 1, UIRectEdgeBottom = 1 << 2, UIRectEdgeRight = 1 << 3, UIRectEdgeAll = UIRectEdgeTop | UIRectEdgeLeft | UIRectEdgeBottom | UIRectEdgeRight } UIRectEdge;
練習 利用UIScreenEdgePanGestureRecognizer手 勢將ViewController頁面打開
用程式驅動Segue
UIViewController Class 啟動segue - (void)performSegueWithIdentifier:(NSString *) identifier sender:(id)sender identifier: 要啟動的segue ID 返回使用Unwind拉線
在storyboard建立segue
設定ID
用程式載入Storyboard中的ViewController
在storyboard放入ViewController並設定ID
利用程式顯示畫面 ThirdViewController *third =[self.storyboard instantiateViewControllerWithIdentifier:@"third"]; [self presentViewController:third animated:YES completion:NULL];
回到上一頁 [self dismissViewControllerAnimated:YES completion:NULL];