//
// ViewController.m
// UI_04_Event
//
// Created by long on 16/7/19.
// Copyright ? 2016年 long. All rights reserved.
//
/**
- iOS事件可分為三類
- 1> 觸摸事件:通過觸摸,手勢進(jìn)行觸發(fā)(例如手指點擊,縮放)
- 2> 運(yùn)動事件:通過加速器進(jìn)行觸發(fā)(例如手機(jī)晃動)
- 3> 遠(yuǎn)程控制事件:通過其他遠(yuǎn)程設(shè)備進(jìn)行觸發(fā)(例如耳機(jī)控制按鈕)
*/
/**
- 響應(yīng)者鏈:
- 檢測: UIApplication --> window --> viewController --> view --> subView
- 響應(yīng): subView --> view --> viewController --> window --> UIApplication
- 在UI控件中,兩類控件默認(rèn)交互是關(guān)閉的,不做任何響應(yīng) 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(@"晃動結(jié)束");
[UIView animateWithDuration:3 animations:^{
self.imageView.alpha = 0.1;
}];
}
//當(dāng)一個手指或者多個手指觸碰屏幕時,會調(diào)用該方法
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
NSLog(@"開始觸摸...");
}
//當(dāng)一個手指或者多個手指在屏幕上移動時,會調(diào)用該方法
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
NSLog(@"移動中...");
UITouch *touch = [touches anyObject];
//獲取當(dāng)前位置
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);
//重新設(shè)置圖片的位置
self.imageView.center = CGPointMake(imageCenter.x + offSet.x, imageCenter.y + offSet.y);
}
//當(dāng)一個手指或者多個手指在屏幕上觸碰結(jié)束時,會調(diào)用該方法
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
NSLog(@"觸摸結(jié)束...");
}
-(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:)];
//設(shè)置手勢點擊次數(shù)
tapGesture.numberOfTapsRequired = 2;
//設(shè)置點擊的手指數(shù)
tapGesture.numberOfTouchesRequired = 2;
[self.view addGestureRecognizer:tapGesture];
/* 2> 長按手勢 */
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
//添加長按手勢到圖片上
[self.imageView addGestureRecognizer:longPressGesture];
//更改長按時間,默認(rèn)是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> 旋轉(zhuǎn)手勢 */
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> 輕掃手勢 */
/**
輕掃手勢默認(rèn)只支持向右滑動,如果想向左滑動,需要再創(chuàng)建一個手勢
*/
//創(chuàng)建一個向右滑動的手勢
UISwipeGestureRecognizer *swipGestureRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipActionRight:)];
[self.view addGestureRecognizer:swipGestureRight];
//創(chuàng)建一個向左滑動的手勢
UISwipeGestureRecognizer *swipGestureLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipActionLeft:)];
//更改direction屬性,默認(rèn)為向右輕掃,現(xiàn)在改為向左輕掃
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> 創(chuàng)建一個UIAlertController控制器
2> 創(chuàng)建UIAlerAction對象
3> 將UIAlerAction對象添加到UIAlertController控制器
4> 推出控制器
*/
//創(chuàng)建UIAlertController控制器
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否刪除此圖片" preferredStyle:UIAlertControllerStyleActionSheet];
//創(chuàng)建UIAlertAction對象--->取消
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"--------");
}];
//創(chuàng)建UIAlertAction對象--->確定
UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
self.imageView.image = nil;
NSLog(@"圖片已經(jīng)刪除");
}];
//創(chuàng)建UIAlertAction對象--->保存
UIAlertAction *save = [UIAlertAction actionWithTitle:@"Save" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"圖片保存到相冊");
//將照片保存到本地相冊 需要回調(diào)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屬性可以改對象的平移,縮放和旋轉(zhuǎn)角度
* 1> 創(chuàng)建"基于控件位置初始位置"的形變
* CGAffineTransformMakeTranslation(平移)
* CGAffineTransformMakeScale(縮放)
* CGAffineTransformMakeRotation(旋轉(zhuǎn))
*
* 2> 創(chuàng)建"基于transform參數(shù)"的形變
* CGAffineTransformTranslate(平移)
* CGAffineTransformScale(縮放)
* CGAffineTransformRotate(旋轉(zhuǎn))
*
* @param scale 系統(tǒng)自帶的縮放比例屬性.直接調(diào)用即可
*
*/
//獲取當(dāng)前的狀態(tài) ---> 縮放中
if (pinchGesture.state == UIGestureRecognizerStateChanged) {
self.imageView.transform = CGAffineTransformMakeScale(pinchGesture.scale, pinchGesture.scale);
}
//縮放結(jié)束
else if (pinchGesture.state == UIGestureRecognizerStateEnded){
//形變結(jié)束的動畫
[UIView animateWithDuration:0.5 animations:^{
//縮放結(jié)束后,取消一切形變
self.imageView.transform = CGAffineTransformIdentity;
}];
}
}
//旋轉(zhuǎn)方法
-(void)rotationAction:(UIRotationGestureRecognizer *)rotationGesture{
//獲取當(dāng)前狀態(tài),是否處于旋轉(zhuǎn)狀態(tài)
if(rotationGesture.state == UIGestureRecognizerStateChanged){
//rotation屬性:旋轉(zhuǎn)手指中改變旋轉(zhuǎn)弧度的屬性
self.imageView.transform = CGAffineTransformMakeRotation(rotationGesture.rotation);
}
//旋轉(zhuǎn)結(jié)束
else if (rotationGesture.state == UIGestureRecognizerStateEnded) {
//動畫,使圖片回到初始狀態(tài)
[UIView animateWithDuration:0.5 animations:^{
self.imageView.transform = CGAffineTransformIdentity;
}];
}
}
//平移方法
-(void)panAction:(UIPanGestureRecognizer )panGesture{
/*
* 拖動照片平移,平移結(jié)束后照片恢復(fù)為初始狀態(tài)
*/
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;
}];
}
}
//輕掃方法
//向右輕掃.循環(huán)照片
-(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];
}
//向左輕掃,循環(huán)照片
-(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];
}
//照片保存到本地相冊是由得回調(diào)方法
-(void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
if (!error) {
NSLog(@"Save success");
}
else{
NSLog(@"Save failed");
}
}
@end