iOS 常用手勢,ios手勢

來源:互聯網
上載者:User

iOS 常用手勢,ios手勢

UIGestureRecognizer 對iOS的各種手勢進行了封裝,完全滿足了使用者對手勢的需求。

以下是對各種手勢的詳細應用和說明,希望能對大家有協助。^_^

 

- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view.    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];        _imageView = [[UIImageView alloc]initWithFrame:CGRectMake((self.view.frame.size.width-150)/2, (self.view.frame.size.height-150)/2, 150, 150)];    _imageView.userInteractionEnabled = YES;//互動使能,允許介面互動    _imageView.image = [UIImage imageNamed:@"cat.png"];    [self.view addSubview:_imageView];        // 單擊的 TapRecognizer    UITapGestureRecognizer *singleTap;    singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(SingleTap:)];    singleTap.numberOfTapsRequired = 1; //點擊的次數 =1 單擊        [_imageView addGestureRecognizer:singleTap];//給對象添加一個手勢監測;        // 雙擊的 TapRecognizer    UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(DoubleTap:)];    doubleTap.numberOfTapsRequired = 2; //點擊的次數 =2 雙擊    [_imageView addGestureRecognizer:doubleTap];//給對象添加一個手勢監測;        /*     1.雙擊手勢確定監測失敗才會觸發單擊手勢的相應操作,否則雙擊時第一擊時會響應單擊事件     2.會造成單擊時要判斷是否是雙擊,調用單擊會有所延時。屬正常現象。     */    [singleTap requireGestureRecognizerToFail:doubleTap];            //捏合縮放手勢 Pinch    UIPinchGestureRecognizer *pinch;    pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlePinch:)];    //    [_imageView addGestureRecognizer:pinch];//添加到_imageView的時候,是要把手指放到_imageView操作    [self.view addGestureRecognizer:pinch];//是self的時候,操作整個view都可以捏合_imageView(在響應事件中操作)    pinch.delegate = self;            //旋轉手勢 Rotation    UIRotationGestureRecognizer *rotateRecognizer = [[UIRotationGestureRecognizer alloc]                                                     initWithTarget:self                                                     action:@selector(handleRotate:)];    [self.view addGestureRecognizer:rotateRecognizer];//是self的時候,操作整個view都可以捏合_imageView(在響應事件中操作)    rotateRecognizer.delegate = self;            //滑動手勢 SwipeRecognizer    UISwipeGestureRecognizer *swipeRecognizer = [[UISwipeGestureRecognizer alloc]                                                 initWithTarget:self                                                 action:@selector(handleSwipe:)];    [self.view addGestureRecognizer:swipeRecognizer];//是self的時候,操作整個view都可以捏合_imageView(在響應事件中操作)    swipeRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;//操作為左滑    swipeRecognizer.delegate = self;        //拖動手勢 PanRecognizer    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc]                                             initWithTarget:self                                             action:@selector(handlePan:)];    [_imageView addGestureRecognizer:panRecognizer];//關鍵語句,添加一個手勢監測;    panRecognizer.maximumNumberOfTouches = 1;    panRecognizer.delegate = self;        //長按手勢 LongPressRecognizer    UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc]                                                         initWithTarget:self                                                         action:@selector(handlelongPress:)];    [_imageView addGestureRecognizer:longPressRecognizer];    longPressRecognizer.minimumPressDuration = 1.0f;//觸發長按事件時間為:1.0秒    longPressRecognizer.delegate = self;    }-(void)SingleTap:(UITapGestureRecognizer*)recognizer{    //處理單擊操作    NSLog(@"單擊操作");}-(void)DoubleTap:(UITapGestureRecognizer*)recognizer{    //處理雙擊操作    NSLog(@"雙擊操作");}- (void)handlePinch:(UIPinchGestureRecognizer*)recognizer{    NSLog(@"縮放操作");//處理縮放操作    //對imageview縮放    _imageView.transform = CGAffineTransformScale(_imageView.transform, recognizer.scale, recognizer.scale);    //對self.view縮放,因為recognizer是添加在self.view上的    //recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);    recognizer.scale = 1;}- (void)handleRotate:(UIRotationGestureRecognizer*) recognizer{    NSLog(@"旋轉操作");//處理旋轉操作    //對imageview旋轉    _imageView.transform = CGAffineTransformRotate(_imageView.transform, recognizer.rotation);    //對self.view旋轉,因為recognizer是添加在self.view上的    //    recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation);    recognizer.rotation = 0;}- (void)handleSwipe:(UISwipeGestureRecognizer*) recognizer{    //處理滑動操作    if(recognizer.direction==UISwipeGestureRecognizerDirectionLeft) {        NSLog(@"左滑滑動操作");    }else if(recognizer.direction==UISwipeGestureRecognizerDirectionRight){        NSLog(@"右滑滑動操作");    }}-(void)handlePan:(UIPanGestureRecognizer*)recognizer{    NSLog(@"拖動操作");    //處理拖動操作,拖動是基於imageview,如果經過旋轉,拖動方向也是相對imageview上下左右移動,而不是螢幕對上下左右    CGPoint translation = [recognizer translationInView:_imageView];    recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,                                         recognizer.view.center.y + translation.y);    [recognizer setTranslation:CGPointZero inView:_imageView];}-(void)handlelongPress:(UILongPressGestureRecognizer*)recognizer{    //處理長按操作,開始結束都會調用,所以長按1次會執行2次    if(recognizer.state == UIGestureRecognizerStateBegan){        NSLog(@"開始長按操作");    }else if(recognizer.state == UIGestureRecognizerStateEnded){        NSLog(@"結束長按操作");    }}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.