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閱讀 229,908評論 6 541
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,324評論 3 429
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,018評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,675評論 1 317
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,417評論 6 412
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,783評論 1 329
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,779評論 3 446
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,960評論 0 290
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,522評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,267評論 3 358
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,471評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,009評論 5 363
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,698評論 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,099評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,386評論 1 294
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,204評論 3 398
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,436評論 2 378

推薦閱讀更多精彩內容