//
// 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