#import "RootViewController.h"
@interface RootViewController ()
1.创建一条UIImageView类的属性
@property(nonatomic, retain)UIImageView *imageView;
@end
@implementation RootViewController
- (void)dealloc
{
[_imageView release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
2.创建一个imageView
self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"12.jpg"]];
self.imageView.frame = CGRectMake(100, 200, 200, 200);
[self.view addSubview:self.imageView];
[_imageView release];
3.打印self.imageView.
NSLog(@"%@", self.imageView);
以上打印结果显示用户交互没有开启.
4.开启用户交互.
self.imageView.userInteractionEnabled = YES;
NSLog(@"%@", self.imageView);
当用户交互开启时,打印控件对象没有userInteractionEnabled = NO这句话(可以根据有没有此话来判断用户交互有没有打开).
如果用户交互没有打开,那么无法对有用户交互要求的控件进行操作.
5.点击.
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
// 把手势加到指定的视图上
[self.imageView addGestureRecognizer:tap];
[tap release];
// 点几下才能触发的方法.
tap.numberOfTapsRequired = 2;
// 点击的时候需要几根手指.
tap.numberOfTouchesRequired = 2;
按住Alt,在视图上双击鼠标两下,触发方法.
6.长按.
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
// 把手势加到imageview上.
[self.imageView addGestureRecognizer:longPress];
[longPress release];
// 设置长按触发的时间.
longPress.minimumPressDuration = 1;
按住Alt,在视图上按住鼠标左键,触发方法.
7.旋转.
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
// 把手势添加到imageview上.
[self.imageView addGestureRecognizer:rotation];
[rotation release];
8.捏合.
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
[self.imageView addGestureRecognizer:pinch];
[pinch release];
9.拖拽.
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
[self.imageView addGestureRecognizer:pan];
[pan release];
10.轻扫.
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
[self.imageView addGestureRecognizer:swipe];
[swipe release];
// 设置轻扫方向.
swipe.direction = UISwipeGestureRecognizerDirectionDown;
在视图的任意位置向设置方向轻扫.
11.屏幕边界手势(只能加载在view上).
UIScreenEdgePanGestureRecognizer *screen = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(screenAction:)];
[self.view addGestureRecognizer:screen];
[screen release];
screen.edges = UIRectEdgeLeft;
}
在view上,向设置的方向滑动.
5).点击触发的方法.
- (void)tapAction:(UITapGestureRecognizer *)tap {
NSLog(@"点击");
}
6).长按触发的方法.
- (void)longPressAction:(UILongPressGestureRecognizer *)longPress {
if (longPress.state == UIGestureRecognizerStateBegan) {
NSLog(@"长按");
}
}
7).旋转触发的方法.
- (void)rotationAction:(UIRotationGestureRecognizer *)rotation {
// 可以通过手势,知道手势添加到哪个视图
UIImageView *imageView = (UIImageView *)rotation.view;
// 找到视图,并且让视图进行旋转.
imageView.transform = CGAffineTransformRotate(imageView.transform, rotation.rotation);
rotation.rotation = 0;
}
8).缩放触发的方法.
- (void)pinchAction:(UIPinchGestureRecognizer *)pinch {
// 先通过手势找视图.
UIImageView *imageView = (UIImageView *)pinch.view;
// 让视图进行缩放.
imageView.transform = CGAffineTransformScale(imageView.transform, pinch.scale, pinch.scale);
NSLog(@"%g", pinch.scale);
pinch.scale = 1;
}
9).拖拽触发的方法.
- (void)panAction:(UIPanGestureRecognizer *)pan {
UIImageView *imageView = (UIImageView *)pan.view;
// 通过手势来获取移动的点.
CGPoint p = [pan translationInView:imageView];
// 通过设置transform实现拖拽
imageView.transform = CGAffineTransformTranslate(imageView.transform, p.x, p.y);
[pan setTranslation:CGPointZero inView:imageView];
}
10).轻扫触发的方法.
- (void)swipeAction:(UISwipeGestureRecognizer *)swipe {
if (swipe.direction == UISwipeGestureRecognizerDirectionDown) {
NSLog(@"向下轻扫!~");
}
}
11).屏幕边界模式触发的方法.
- (void)screenAction:(UIScreenEdgePanGestureRecognizer *)screen {
NSLog(@"屏幕边界模式开启");
}