生活随笔
收集整理的這篇文章主要介紹了
IOS - 七大手势操作
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#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);
//設置比例 為 1
pinch.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);
//將旋轉角度 置為 0
rote.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 - 七大手势操作的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。