標籤:支援 建立對象 ssd 事件 旋轉 應該 一個 atom 狀態
文章只要你有一點點基礎應該就可以看的懂,文章只為學習交流
#import "ViewController.h"@interface ViewController ()@property (nonatomic,retain)UIImageView *imageView;@property (nonatomic,assign)NSInteger index;//下標@property (nonatomic,retain)NSMutableArray *images;//圖片名 字數組@end@implementation ViewController#載入視圖-(void)viewDidLoad{[super viewDidLoad];//布局imageView[self layoutImageView];//1.建立輕拍手勢[self tapGestureRecognizer];//2.建立清掃手勢[self swipeGestureRecognizer];//3.建立長安手勢[self longPressGestureRecognizer];//4.建立平移手勢[self panGestureTecognizer];//5.建立捏合手勢[self pinchGestureRecognizer];//6.建立 旋轉手勢[self rotationGestureRecognizer]//7.建立側邊手勢[self screenEdgePanGestureRecognizer];
}
##布局ImageView-(void)layoutImageView{//建立對象UIImageView *imageView =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 375, 667)];//配置屬性imageView.backgroundColor =[UIColor purpleColor];//設定圖片imageView.image =[UIImage imageNamed:@"car1.jpg"];//添加父視圖[self.view addSubview:imageView];//將建立的圖片視圖對象 給屬性賦值self.imageView =imageView;//開啟使用者互動self.imageView.userInteractionEnabled =YES;self.images =[NSMutableArray array];for (int i = 1; i<9; i++) { NSString * imageName =[NSString stringWithFormat:@"car%d.jpg",i]; [_images addObject:imageName];}// _index =1;}#pragma 輕怕手勢//建立輕拍手勢-(void)tapGestureRecognizer{//建立手勢對象UITapGestureRecognizer *tap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];//配置屬性//輕拍次數tap.numberOfTapsRequired =1;//輕拍手指個數tap.numberOfTouchesRequired =1;//講手勢添加到指定的視圖上[_imageView addGestureRecognizer:tap];}//輕拍事件-(void)tapAction:(UITapGestureRecognizer *)tap{//圖片切換NSLog(@"拍一下");_index ++;if (_index == 8) { _index = 1;}self.imageView.image =[UIImage imageNamed:_images[_index]];}#pragma 清掃手勢//清掃手勢-(void)swipeGestureRecognizer{UISwipeGestureRecognizer *swipe =[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];//配置屬性//一個清掃手勢 只能有兩個方向(上和下) 或者 (左或右)//如果想支援上下左右清掃 那麼一個手勢不能實現 需要建立兩個手勢swipe.direction =UISwipeGestureRecognizerDirectionLeft;//添加到指定視圖[_imageView addGestureRecognizer:swipe];UISwipeGestureRecognizer *swipe2 =[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];swipe2.direction =UISwipeGestureRecognizerDirectionRight;[_imageView addGestureRecognizer:swipe2];}//清掃事件-(void)swipeAction:(UISwipeGestureRecognizer *)swipe{NSLog(@"掃一下");if (swipe.direction ==UISwipeGestureRecognizerDirectionRight) { NSLog(@"右清掃"); _index --; if (_index < 1) { _index =7; } _imageView.image =[UIImage imageNamed:_images[_index]];}else if(swipe.direction == UISwipeGestureRecognizerDirectionLeft){ NSLog(@"左掃一下"); _index ++; if (_index == 8) { _index=1; } _imageView.image =[UIImage imageNamed:_images[_index]];}}#pragma 長按手勢//建立長按手勢-(void)longPressGestureRecognizer{UILongPressGestureRecognizer *longPress =[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];//最短長按時間longPress.minimumPressDuration =2;//允許移動最大距離longPress.allowableMovement =1;//添加到指定視圖[_imageView addGestureRecognizer:longPress];}//長按事件-(void)longPressAction:(UILongPressGestureRecognizer *)longPress{//NSLog(@"長按");//對於長安有開始和 結束狀態if (longPress.state == UIGestureRecognizerStateBegan) { NSLog(@"長按開始"); //將圖片儲存到相簿 UIImage *image =[UIImage imageNamed:_images[_index]]; UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);}else if (longPress.state == UIGestureRecognizerStateEnded){ NSLog(@"長按結束");}}#pragma 平移手勢//建立平移手勢-(void)panGestureTecognizer{UIPanGestureRecognizer *pan =[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];//添加到指定視圖[_imageView addGestureRecognizer:pan];}//建立平移事件-(void)panAction:(UIPanGestureRecognizer *)pan{//擷取手勢的位置CGPoint position =[pan translationInView:_imageView];//通過stransform 進行平移交換_imageView.transform = CGAffineTransformTranslate(_imageView.transform, position.x, position.y);//將增量置為零[pan setTranslation:CGPointZero inView:_imageView];}#pragma 捏合手勢-(void)pinchGestureRecognizer{UIPinchGestureRecognizer *pinch =[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)];//添加到指定視圖[_imageView addGestureRecognizer:pinch];}//添加捏合事件-(void)pinchAction:(UIPinchGestureRecognizer *)pinch{//通過 transform(改變) 進行視圖的視圖的捏合_imageView.transform =CGAffineTransformScale(_imageView.transform, pinch.scale, pinch.scale);//設定比例 為 1pinch.scale = 1;}#pragma 旋轉手勢//建立旋轉手勢-(void)rotationGestureRecognizer{UIRotationGestureRecognizer *rotation =[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAction:)];//添加到指定的視圖[_imageView addGestureRecognizer:rotation];}//旋轉事件-(void)rotationAction:(UIRotationGestureRecognizer *)rote{//通過transform 進行旋轉變換_imageView.transform = CGAffineTransformRotate(_imageView.transform, rote.rotation);//將旋轉角度 置為 0rote.rotation = 0;}#pragma 側邊手勢//建立側邊手勢-(void)screenEdgePanGestureRecognizer{UIScreenEdgePanGestureRecognizer *screenPan = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(screenPanAction:)];[_imageView addGestureRecognizer:screenPan];}//建立邊緣事件-(void)screenPanAction:(UIScreenEdgePanGestureRecognizer *)screenPan{NSLog(@"邊緣");}- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.}@end
iOS學習必須瞭解的七大手勢