iOS事件/手势

//
// ViewController.m
// UI_04_Event
//
// Created by long on 16/7/19.
// Copyright © 2016年 long. All rights reserved.
//

/**

  • iOS事件可分为三类
  • 1> 触摸事件:通过触摸,手势进行触发(例如手指点击,缩放)
  • 2> 运动事件:通过加速器进行触发(例如手机晃动)
  • 3> 远程控制事件:通过其他远程设备进行触发(例如耳机控制按钮)
    */

/**

  • 响应者链:
  • 检测: UIApplication --> window --> viewController --> view --> subView
  • 响应: subView --> view --> viewController --> window --> UIApplication
  • 在UI控件中,两类控件默认交互是关闭的,不做任何响应 UIImageView和UILabel
    */

import "ViewController.h"

@interface ViewController ()
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) NSMutableArray *mArr;
@end

@implementation ViewController

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.jpg"]];
[self.view addSubview:self.imageView];

[self addGesture];

}

//晃动事件
-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{
NSLog(@"开始晃动");

int random = arc4random() % (5 - 1 + 1) + 1;
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",random]];
self.imageView.image = nil; //避免图片重叠
self.imageView.image = image;

}
-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{
NSLog(@"晃动结束");

[UIView animateWithDuration:3 animations:^{
    self.imageView.alpha = 0.1;
}];

}

//当一个手指或者多个手指触碰屏幕时,会调用该方法
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
NSLog(@"开始触摸...");
}
//当一个手指或者多个手指在屏幕上移动时,会调用该方法
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
NSLog(@"移动中...");

UITouch *touch = [touches anyObject];
//获取当前位置
CGPoint currentPoint = [touch locationInView:self.view];
//获取前一个位置
CGPoint previousPoint = [touch previousLocationInView:self.view];
//获取图片原位置
CGPoint imageCenter = self.imageView.center;
//求出偏移量
CGPoint offSet = CGPointMake(currentPoint.x - previousPoint.x, currentPoint.y - previousPoint.y);
//重新设置图片的位置
self.imageView.center = CGPointMake(imageCenter.x + offSet.x, imageCenter.y + offSet.y);

}
//当一个手指或者多个手指在屏幕上触碰结束时,会调用该方法
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
NSLog(@"触摸结束...");
}

-(void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

}

//延迟加载(懒加载)
-(NSMutableArray *)mArr{
if (_mArr == nil) {
_mArr = [NSMutableArray array];
}
return _mArr;
}

pragma -mark 添加的手势

-(void)addGesture{
/* 1> 轻拍手势 */
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
//设置手势点击次数
tapGesture.numberOfTapsRequired = 2;
//设置点击的手指数
tapGesture.numberOfTouchesRequired = 2;

[self.view addGestureRecognizer:tapGesture];


/* 2> 长按手势 */
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
//添加长按手势到图片上
[self.imageView addGestureRecognizer:longPressGesture];
//更改长按时间,默认是0.5秒,一般不改
longPressGesture.minimumPressDuration = 0.5;

// 打开imageView的交互
self.imageView.userInteractionEnabled = YES;


/*3> 缩放手势 */
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
[self.imageView addGestureRecognizer:pinchGesture];

/*4> 旋转手势 */
UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
[self.imageView addGestureRecognizer:rotationGesture];


/*5. 平移手势 */
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
[self.imageView addGestureRecognizer:panGesture];


/*6> 轻扫手势 */
/**
    轻扫手势默认只支持向右滑动,如果想向左滑动,需要再创建一个手势
 */
//创建一个向右滑动的手势
UISwipeGestureRecognizer *swipGestureRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipActionRight:)];
[self.view addGestureRecognizer:swipGestureRight];

//创建一个向左滑动的手势
UISwipeGestureRecognizer *swipGestureLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipActionLeft:)];
//更改direction属性,默认为向右轻扫,现在改为向左轻扫
swipGestureLeft.direction =  UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipGestureLeft];

for (int i = 0; i < 5; i++) {
    UIImage *image = [UIImage imageNamed: [NSString stringWithFormat:@"%d.jpg",i+1]];
    [self.mArr addObject:image];
}

}

pragma -mark 手势的方法

//轻拍方法
-(void)tapAction:(UITapGestureRecognizer *)tapGesture{
NSLog(@"tapGesture...");
}
//长按方法
-(void)longPressAction:(UILongPressGestureRecognizer *)longPressGesture{

/**
 *  提示框控制器:
    1> 创建一个UIAlertController控制器
    2> 创建UIAlerAction对象
    3> 将UIAlerAction对象添加到UIAlertController控制器
    4> 推出控制器
 */
//创建UIAlertController控制器
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否删除此图片" preferredStyle:UIAlertControllerStyleActionSheet];

//创建UIAlertAction对象--->取消
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    NSLog(@"--------");
}];
//创建UIAlertAction对象--->确定
UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
    self.imageView.image = nil;
    NSLog(@"图片已经删除");
}];
//创建UIAlertAction对象--->保存
UIAlertAction *save = [UIAlertAction actionWithTitle:@"Save" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
    NSLog(@"图片保存到相册");
    
    //将照片保存到本地相册 需要回调image:didFinishSavingWithError:contextInfo:方法
    UIImageWriteToSavedPhotosAlbum(self.imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
    
    
}];

//添加UIAlertAction对象到UIAlertController控制器
[alert addAction:cancel];
[alert addAction:ok];
[alert addAction:save];

//推出UIAlertController控制器
[self presentViewController:alert animated:YES completion:nil];

//长按动画,将照片移动到view中心位置
[UIView animateWithDuration:0.3 animations:^{
    self.imageView.center = self.view.center;
}];

}

//缩放方法
-(void)pinchAction:(UIPinchGestureRecognizer )pinchGesture{
/
*
* 在缩放手势的使用的缩放方法
*
*
* @param transform transform属性可以改对象的平移,缩放和旋转角度
* 1> 创建"基于控件位置初始位置"的形变
* CGAffineTransformMakeTranslation(平移)
* CGAffineTransformMakeScale(缩放)
* CGAffineTransformMakeRotation(旋转)
*
* 2> 创建"基于transform参数"的形变
* CGAffineTransformTranslate(平移)
* CGAffineTransformScale(缩放)
* CGAffineTransformRotate(旋转)
*
* @param scale 系统自带的缩放比例属性.直接调用即可
*
*/

//获取当前的状态 ---> 缩放中
if (pinchGesture.state == UIGestureRecognizerStateChanged) {
    self.imageView.transform = CGAffineTransformMakeScale(pinchGesture.scale, pinchGesture.scale);
}
//缩放结束
else if (pinchGesture.state == UIGestureRecognizerStateEnded){
    //形变结束的动画
    [UIView animateWithDuration:0.5 animations:^{
        //缩放结束后,取消一切形变
        self.imageView.transform = CGAffineTransformIdentity;
    }];
}

}

//旋转方法
-(void)rotationAction:(UIRotationGestureRecognizer *)rotationGesture{
//获取当前状态,是否处于旋转状态
if(rotationGesture.state == UIGestureRecognizerStateChanged){
//rotation属性:旋转手指中改变旋转弧度的属性
self.imageView.transform = CGAffineTransformMakeRotation(rotationGesture.rotation);
}
//旋转结束
else if (rotationGesture.state == UIGestureRecognizerStateEnded) {
//动画,使图片回到初始状态
[UIView animateWithDuration:0.5 animations:^{
self.imageView.transform = CGAffineTransformIdentity;
}];
}
}

//平移方法
-(void)panAction:(UIPanGestureRecognizer )panGesture{
/
*
* 拖动照片平移,平移结束后照片恢复为初始状态
*/
if (panGesture.state == UIGestureRecognizerStateChanged) {
CGPoint offset = [panGesture translationInView:self.view];
self.imageView.transform = CGAffineTransformMakeTranslation(offset.x, offset.y);
}else if (panGesture.state == UIGestureRecognizerStateEnded){
[UIView animateWithDuration:0.5 animations:^{
self.imageView.transform = CGAffineTransformIdentity;
}];
}

}

//轻扫方法
//向右轻扫.循环照片
-(void)swipActionRight:(UISwipeGestureRecognizer *)swipGestureRight{
NSUInteger index = [self.mArr indexOfObject:self.imageView.image];
index++;
if (index == self.mArr.count-1) {
NSLog(@"这是最一张照片");
index = 0;
}
self.imageView.image = self.mArr[index];
}
//向左轻扫,循环照片
-(void)swipActionLeft:(UISwipeGestureRecognizer *)swipGestureLeft{
NSInteger index = [self.mArr indexOfObject:self.imageView.image];
index--;
if (index < 0) {
NSLog(@"这是第一张照片");
index = self.mArr.count-1;
}
self.imageView.image = self.mArr[index];
}

//照片保存到本地相册是由得回调方法
-(void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
if (!error) {
NSLog(@"Save success");
}
else{
NSLog(@"Save failed");
}
}

@end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,227评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,755评论 1 298
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,899评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,257评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,617评论 3 288
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,757评论 1 221
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,982评论 2 315
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,715评论 0 204
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,454评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,666评论 2 249
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,148评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,512评论 3 258
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,156评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,112评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,896评论 0 198
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,809评论 2 279
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,691评论 2 272

推荐阅读更多精彩内容