UI总结-手势
常用的6种手势:
#import "ViewController.h"
@interface ViewController ()
@property(nonatomic, retain)UIImageView *imagev;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor whiteColor];
self.imagev = [[UIImageView alloc]init];
self.imagev.frame = CGRectMake(50, 200, 350, 300);
[self.view addSubview:self.imagev];
[_imagev release];
self.imagev.image = [UIImage imageNamed:@"818006c62c85ebc153a7b5a47c5efbd3.jpg"];
//在给图片加手势等操作时,需要先打开对象的用户交互
self.imagev.userInteractionEnabled = YES;
//1.轻点手势
//创建手势
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
//给试图添加手势
[self.imagev addGestureRecognizer:tap];
[tap release];
//两个属性
//点击几下触发方法
tap.numberOfTapsRequired = 2;
//用几个手指点触发方法
tap.numberOfTouchesRequired = 1;
//2.长按手势
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];
[self.imagev addGestureRecognizer:longPress];
[longPress release];
//属性,长按多长时间触发方法
longPress.minimumPressDuration = 4;
//3.旋转手势
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)];
[self.imagev addGestureRecognizer:rotation];
[rotation release];
//4.捏合手势
UIPinchGestureRecognizer *pin = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinAction:)];
[self.imagev addGestureRecognizer:pin];
[pin release];
//5.拖拽手势
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
[self.imagev addGestureRecognizer:pan];
[pan release];
//6.轻扫手势
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];
[self.imagev addGestureRecognizer:swipe];
[swipe release];
}
#pragma mark 轻点手势的点击语法
-(void)tapAction:(UITapGestureRecognizer *)tap{
NSLog(@"图片完成点击");
}
-(void)longPressAction:(UILongPressGestureRecognizer *)longPress{
//通过手势的状态,避免重复的触发手势方法
if (longPress.state == UIGestureRecognizerStateBegan) {
NSLog(@"长按触发");
}
}
#pragma mark 旋转手势的点击语法
-(void)rotation:(UIRotationGestureRecognizer *)ro{
self.imagev.transform = CGAffineTransformMakeRotation(ro.rotation);
}
#pragma mark 捏合手势的点击语法
-(void)pinAction:(UIPinchGestureRecognizer *)pinch{
self.imagev.transform = CGAffineTransformMakeScale(pinch.scale, pinch.scale);
}
#pragma mark 拖拽手势的点击语法
-(void)panAction:(UIPanGestureRecognizer *)pan{
//当前手势进过的坐标
CGPoint point = [pan translationInView:self.imagev];
//self.imagev.transform = CGAffineTransformMakeTranslation(point.x, point.y);
self.imagev.center = CGPointMake(self.imagev.center.x + point.x , self.imagev.center.y + point.y);
[pan setTranslation:CGPointZero inView:self.imagev];
}
#pragma mark 轻扫手势的点击语法
-(void)swipeAction:(UISwipeGestureRecognizer *)swipe{
NSLog(@"轻扫触发");
}
运行结果如下图所示: