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.}
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。