- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor yellowColor];
// 1.創(chuàng)建一個UIImage
// 通過文件名直接進行創(chuàng)建
// UIImage *image = [UIImage imageNamed:@"Curry.png"];
//
// UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
// imageView.frame = CGRectMake(100, 100, 100, 200);
// [self.view addSubview:imageView];
//
// [imageView release];
UIImage *image = [UIImage imageNamed:@"zuozuomuxi.jpg"];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 160, 360, 360)];
imageView.image = image;
[self.view addSubview:imageView];
[imageView release];
// UIImageView相當(dāng)于相框,用來顯示,UIImage顯示的內(nèi)容
// 把圖片的交互打開
imageView.userInteractionEnabled = YES;
// 手勢 Gesture
// 1.輕拍Tap
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
// 設(shè)置需要點擊幾次才會觸發(fā)方法
tap.numberOfTapsRequired = 2;
tap.numberOfTouchesRequired = 2;
// 把手勢加入到圖片上
[imageView addGestureRecognizer:tap];
// 內(nèi)存管理
[tap release];
// 2.長按 LongPress
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
// 設(shè)置一下長按需要的最短時間
longPress.minimumPressDuration = 3;
// 判斷在長按過程中允許手指移動的距離
longPress.allowableMovement = 100;
[imageView addGestureRecognizer:longPress];
[longPress release];
// 3.旋轉(zhuǎn) rotation
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
[imageView addGestureRecognizer:rotation];
[rotation release];
// 4.捏合 pinch
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
[imageView addGestureRecognizer:pinch];
[pinch release];
// 5.拖拽 pan
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
[imageView addGestureRecognizer:pan];
[pan release];
// 6.輕掃
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
[imageView addGestureRecognizer:swipe];
[swipe release];
// 輕掃的方向
swipe.direction = UISwipeGestureRecognizerDirectionLeft;
}
- (void)tapAction:(id)sender {
NSLog(@"taped an image");
}
- (void)longPress:(UILongPressGestureRecognizer *)longPress {
// 長按的方法在手勢的各個狀態(tài)中都會進行觸發(fā),所以需要進行判斷
if (longPress.state == UIGestureRecognizerStateBegan) {
NSLog(@"開始長按了");
}
NSLog(@"long pressed");
}
- (void)rotationAction:(UIRotationGestureRecognizer *)rotationGesture {
// 獲得添加手勢的視圖
UIImageView *imageView = (UIImageView *)[rotationGesture view];
// 調(diào)整視圖的transform屬性(順時針正數(shù),逆時針負數(shù))
imageView.transform = CGAffineTransformMakeRotation(rotationGesture.rotation);
}
- (void)pinchAction:(UIPinchGestureRecognizer *)pinchGesture {
UIImageView *imageView = (UIImageView *)[pinchGesture view];
imageView.transform = CGAffineTransformMakeScale(pinchGesture.scale, pinchGesture.scale);
// imageView.transform = CGAffineTransformScale(imageView.transform, pinchGesture.scale, pinchGesture.scale);
// pinchGesture.scale = 1;
}
- (void)panAction:(UIPanGestureRecognizer *)panGesture {
// 獲取拖拽手勢添加的視圖
UIImageView *imageView = (UIImageView *)[panGesture view];
// 獲取手勢經(jīng)過的點
CGPoint p = [panGesture translationInView:imageView];
// 然后對視圖的transform屬性進行改變(橫移x變y不變 豎移x不變y變)
imageView.transform = CGAffineTransformMakeTranslation(p.x, p.y);
// imageView.transform = CGAffineTransformTranslate(imageView.transform, p.x, p.y);
// [panGesture setTranslation:CGPointZero inView:imageView];
}
- (void)swipeAction:(UISwipeGestureRecognizer *)swipeGesture {
if (swipeGesture.direction == UISwipeGestureRecognizerDirectionLeft) {
NSLog(@"向左");
} else if (swipeGesture.direction == UISwipeGestureRecognizerDirectionRight) {
NSLog(@"向右");
} else if (swipeGesture.direction == UISwipeGestureRecognizerDirectionUp) {
NSLog(@"向上");
} else if (swipeGesture.direction == UISwipeGestureRecognizerDirectionDown) {
NSLog(@"向下");
}
}
以上是關(guān)于手勢的一些使用硝烂,先記一下以防以后忘記址否。
另外需要注意: 一個手勢只能添加到一個view上 一個view可以添加多個手勢 估計和一個view只能有一個superview一樣 一個guesture也只能對應(yīng)一個view吧. 猜得.