手勢

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic,retain)UIImageView *imageView;
@property (nonatomic,assign)NSInteger index;//下標
@property (nonatomic,retain)NSMutableArray *images;//圖片名    字數(shù)組

@end

@implementation ViewController

加載視圖

-(void)viewDidLoad
{

[super viewDidLoad];
//布局imageView
[self layoutImageView];
//1.創(chuàng)建輕拍手勢
[self tapGestureRecognizer];
//2.創(chuàng)建清掃手勢
[self swipeGestureRecognizer];
//3.創(chuàng)建長安手勢
[self longPressGestureRecognizer];
/ /4.創(chuàng)建平移手勢
[self panGestureTecognizer];
//5.創(chuàng)建捏合手勢
[self pinchGestureRecognizer];
//6.創(chuàng)建 旋轉手勢
[self rotationGestureRecognizer]
//7.創(chuàng)建邊緣手勢
[self screenEdgePanGestureRecognizer];
}

布局ImageView

-(void)layoutImageView
{

//創(chuàng)建對象
UIImageView *imageView =[[UIImageView     alloc]initWithFrame:CGRectMake(0, 0, 375, 667)];
//配置屬性
imageView.backgroundColor =[UIColor purpleColor];
//設置圖片
imageView.image =[UIImage imageNamed:@"car1.jpg"];
//添加父視圖
[self.view addSubview:imageView];
//將創(chuàng)建的圖片視圖對象 給屬性賦值
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 輕怕手勢

//創(chuàng)建輕拍手勢
-(void)tapGestureRecognizer
{

//創(chuàng)建手勢對象
UITapGestureRecognizer *tap =[[UITapGestureRecognizer alloc]initWithTarget:self     action:@selector(tapAction:)];
//配置屬性
//輕拍次數(shù)
tap.numberOfTapsRequired =1;
//輕拍手指個數(shù)
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:)];
//配置屬性
//一個清掃手勢  只能有兩個方向(上和下) 或者 (左或右)
//如果想支持上下左右清掃  那么一個手勢不能實現(xiàn)  需要創(chuàng)建兩個手勢
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 長按手勢

//創(chuàng)建長按手勢
-(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(@"長按");
//對于長安有開始和 結束狀態(tài)
if (longPress.state == UIGestureRecognizerStateBegan) {
    NSLog(@"長按開始");
    //將圖片保存到相冊
    UIImage *image =[UIImage imageNamed:_images[_index]];
    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
}else if (longPress.state == UIGestureRecognizerStateEnded)
{
    NSLog(@"長按結束");
}

}

pragma 平移手勢

//創(chuàng)建平移手勢
-(void)panGestureTecognizer
{

UIPanGestureRecognizer *pan =[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
//添加到指定視圖
[_imageView addGestureRecognizer:pan];

}
//創(chuàng)建平移事件
-(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 旋轉手勢

//創(chuàng)建旋轉手勢
-(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 邊緣手勢

//創(chuàng)建邊緣手勢
-(void)screenEdgePanGestureRecognizer
{

UIScreenEdgePanGestureRecognizer *screenPan = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(screenPanAction:)];
[_imageView addGestureRecognizer:screenPan];

}
//創(chuàng)建邊緣事件
-(void)screenPanAction:(UIScreenEdgePanGestureRecognizer *)screenPan
{

NSLog(@"邊緣");

}


- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容