UI_手勢,ui手勢

來源:互聯網
上載者:User

UI_手勢,ui手勢
輕拍手勢

    // 建立一個輕拍手勢,同時綁定了事件    UITapGestureRecognizer *aTapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGRAction:)];    // 設定輕拍次數    aTapGR.numberOfTapsRequired = 1;    // 設定手指觸摸的個數    aTapGR.numberOfTouchesRequired = 1; // 模擬器按住 alt 可以兩個手指觸摸    // 添加手勢    [self.rootView addGestureRecognizer:aTapGR];    [aTapGR release];#pragma mark - 輕拍手勢- (void)tapGRAction:(UITapGestureRecognizer *)sender{    [self.rootView.aTextField resignFirstResponder];    // 下面也可以,讓rootVIew 停止編輯    // [self.rootView endEditing:YES];}
長按手勢
    UILongPressGestureRecognizer *longPressGR = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGRAction:)];    [self.rootView addGestureRecognizer:longPressGR];    [longPressGR release];#pragma mark - 長按手勢- (void)longPressGRAction:(UILongPressGestureRecognizer *)sender{    // 判斷,否則會變兩次    if (sender.state == UIGestureRecognizerStateBegan) {        CGFloat red = arc4random() % 256 / 255.0;        CGFloat green = arc4random() % 256 / 255.0;        CGFloat blue = arc4random() % 256 / 255.0;        self.rootView.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:1];    }}
旋轉手勢
    UIRotationGestureRecognizer *rotationGR = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGRAction:)];    // 手勢要添加到圖片上    [self.rootView.opeImageView addGestureRecognizer:rotationGR];    [rotationGR release];#pragma mark - 旋轉手勢- (void)rotationGRAction:(UIRotationGestureRecognizer *)sender{    // 操作對象和旋轉角度    self.rootView.opeImageView.transform = CGAffineTransformRotate(self.rootView.opeImageView.transform, sender.rotation);    // 旋轉后角度為0    sender.rotation = 0;    // 可以代替上面兩行    // self.rootView.opeImageView.transform = CGAffineTransformMakeRotation(sender.rotation);}
捏合手勢
    UIPinchGestureRecognizer *pinchGR = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGRAction:)];    [self.rootView.opeImageView addGestureRecognizer:pinchGR];    [pinchGR release];#pragma mark - 捏合手勢- (void)pinchGRAction:(UIPinchGestureRecognizer *)sender{    self.rootView.opeImageView.transform = CGAffineTransformScale(self.rootView.opeImageView.transform, sender.scale, sender.scale);    // 縮放完成後縮放比例重新置為1    sender.scale = 1;}
平移手勢
    UIPanGestureRecognizer *panGR = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGRAction:)];    [self.rootView.opeImageView addGestureRecognizer:panGR];    [panGR release];#pragma mark - 平移手勢- (void)panGRAction:(UIPanGestureRecognizer *)sender{    CGPoint point = [sender translationInView:sender.view];    self.rootView.opeImageView.transform = CGAffineTransformTranslate(self.rootView.opeImageView.transform, point.x, point.y);    // 平移之後讓手勢相對於所在的 view 重新置0    [sender setTranslation:CGPointZero inView:sender.view];}
撥動手勢
    UISwipeGestureRecognizer *swipeGR = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGRAction:)];    [self.rootView addGestureRecognizer:swipeGR];    [swipeGR release];    // 一個手勢只能一個方向輕掃,多個方向寫多個手勢    swipeGR.direction = UISwipeGestureRecognizerDirectionRight;    /**     typedef NS_OPTIONS(NSUInteger, UISwipeGestureRecognizerDirection) {     UISwipeGestureRecognizerDirectionRight = 1 << 0,     UISwipeGestureRecognizerDirectionLeft  = 1 << 1,     UISwipeGestureRecognizerDirectionUp    = 1 << 2,     UISwipeGestureRecognizerDirectionDown  = 1 << 3     };     */#pragma mark - 清掃手勢- (void)swipeGRAction:(UISwipeGestureRecognizer *)sender{    NSLog(@"清掃手勢");}
螢幕邊緣輕掃
    UIScreenEdgePanGestureRecognizer *screenEdgePanGR = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(screenEdgePanGRAction:)];    [self.rootView addGestureRecognizer:screenEdgePanGR];    [screenEdgePanGR release];#pragma mark - 螢幕邊緣清掃手勢- (void)screenEdgePanGRAction:(UIScreenEdgePanGestureRecognizer *)sender{    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.