ios之UIGestureRecognizer手勢基礎使用解析

來源:互聯網
上載者:User

標籤: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手勢基礎使用解析

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.