標籤:style io ar color os 使用 sp for 檔案
UIGestureRecognizer 的子類分別有很多手勢,通過 不用的手勢可以執行不同的操作,下面來介紹下他們的基本使用方法所有手勢配置基本相同,只是針對不同的手勢裡面有部分屬性可以設定,比如說tap點進去看他有兩個參數可以設定一個是點擊次數,和點擊手指數可設定。如果不知道這個手勢能配置說明參數,那麼點擊進入相應的.h 檔案查看
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(processGestureRecongnizer:)];//點選手勢 [tapGesture setNumberOfTouchesRequired:1]; [tapGesture setNumberOfTapsRequired:1]; [_view addGestureRecognizer:tapGesture]; [tapGesture release];
- (void)processGestureRecongnizer:(UIGestureRecognizer *)gesture{ if ([gesture isKindOfClass:[UITapGestureRecognizer class]]) { [self positionAnimation]; }}
#pragma mark -- InitUserInterface- (void)initUserInterface{ _view = [[UIView alloc]init]; [_view setBounds:CGRectMake(0, 0, 200, 200)]; [_view setCenter:CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds))]; [_view setBackgroundColor:[UIColor grayColor]]; [self.view addSubview:_view]; //兩手指撥動手勢 UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(processGestureRecognizer:)]; [pinch setDelegate:self];//設定代理 [_view addGestureRecognizer:pinch]; //對view添加這個手勢 [pinch release]; //旋轉手勢 UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(processGestureRecognizer:)]; [rotation setDelegate:self]; [_view addGestureRecognizer:rotation]; [rotation release]; //長按手勢 UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(processGestureRecognizer:)]; longPress.minimumPressDuration = 2; [_view addGestureRecognizer:longPress]; [longPress release]; //滑動手勢--左
UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(processGestureRecognizer:)]; [leftSwipe setDirection:UISwipeGestureRecognizerDirectionLeft];//配置滑動方向 [self.view addGestureRecognizer:leftSwipe]; [leftSwipe release]; //滑動手勢--右 UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(processGestureRecognizer:)]; [rightSwipe setDirection:UISwipeGestureRecognizerDirectionRight]; [self.view addGestureRecognizer:rightSwipe]; [rightSwipe release]; //拖移手勢 UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(processGestureRecognizer:)]; [pan setDelegate:self]; [_view addGestureRecognizer:pan]; [pan release]; }#pragma mark -- GestureRrecognizer methods//代理方法- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{ return NO;}// 手勢action 讓對應的手勢執行相應地操作- (void)processGestureRecognizer:(UIGestureRecognizer *)gesture{ //判斷手勢類型 if ([gesture isKindOfClass:[UIPinchGestureRecognizer class]]) { UIPinchGestureRecognizer *pinch = (UIPinchGestureRecognizer *)gesture; static float lastScale;//靜態變數記錄上次大小 if (pinch.state == UIGestureRecognizerStateBegan) { lastScale = pinch.scale;// 手勢開始把初始scale賦給靜態變數方便更新 }else if (pinch.state == UIGestureRecognizerStateChanged){ [_view setTransform:CGAffineTransformScale(_view.transform, 1+(pinch.scale - lastScale), 1+(pinch.scale - lastScale))];//讓View進行動態放大或縮小 lastScale = pinch.scale;// 更新scale的值 --(這樣做讓view保持變化後的狀態而不會是初始狀態) //此方法不需要更新lastScale的值 都是從原型開始// [_view setTransform:CGAffineTransformMakeScale(1+(pinch.scale - lastScale), 1+(pinch.scale - lastScale))]; } }else if ([gesture isKindOfClass:[UIRotationGestureRecognizer class]]) { UIRotationGestureRecognizer *rotation = (UIRotationGestureRecognizer *)gesture; static float lastRotation; if (rotation.state == UIGestureRecognizerStateBegan) { lastRotation = rotation.rotation; }else if (rotation.state == UIGestureRecognizerStateChanged){ [_view setTransform:CGAffineTransformRotate(_view.transform, rotation.rotation - lastRotation)]; lastRotation = rotation.rotation; } } else if ([gesture isKindOfClass:[UILongPressGestureRecognizer class]]) { UILongPressGestureRecognizer *longPress = (UILongPressGestureRecognizer *)gesture; if (longPress.state == UIGestureRecognizerStateBegan) { UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"溫馨提示" message:@"Long Press Begin" delegate:self cancelButtonTitle:@"確定" otherButtonTitles: nil]; [alert show]; [alert release]; } } else if ([gesture isKindOfClass:[UISwipeGestureRecognizer class]]) { UISwipeGestureRecognizer *swipe = (UISwipeGestureRecognizer *)gesture; if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) { [UIView animateWithDuration:1 animations:^{ [_view setCenter:CGPointMake(CGRectGetMinX(self.view.bounds), CGRectGetMidY(self.view.bounds))]; }]; }else{ [UIView animateWithDuration:1 animations:^{ [_view setCenter:CGPointMake(CGRectGetMaxX(self.view.bounds), CGRectGetMidY(self.view.bounds))]; }]; } } else if ([gesture isKindOfClass:[UIPanGestureRecognizer class]]) { UIPanGestureRecognizer *pan = (UIPanGestureRecognizer *)gesture; static CGPoint lastLocation; if (pan.state == UIGestureRecognizerStateBegan) { lastLocation = _view.center; }else if (pan.state == UIGestureRecognizerStateChanged) { CGPoint translationPoint = [pan translationInView:self.view]; _view.center = CGPointMake(translationPoint.x + lastLocation.x, translationPoint.y+ lastLocation.y); }else if (pan.state == UIGestureRecognizerStateEnded) { lastLocation = CGPointZero; } }}
ios之UIGestureRecognizer手勢基礎使用解析