// 创建一个轻拍手势,同时绑定了事件
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.
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/yadong_zhao/article/details/46755523