iOS事件/手勢

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末本昏,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子州丹,更是在濱河造成了極大的恐慌,老刑警劉巖板驳,帶你破解...
    沈念sama閱讀 221,820評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件心俗,死亡現(xiàn)場離奇詭異流酬,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)甘畅,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,648評論 3 399
  • 文/潘曉璐 我一進(jìn)店門埂蕊,熙熙樓的掌柜王于貴愁眉苦臉地迎上來实夹,“玉大人,你說我怎么就攤上這事粒梦×梁剑” “怎么了?”我有些...
    開封第一講書人閱讀 168,324評論 0 360
  • 文/不壞的土叔 我叫張陵匀们,是天一觀的道長缴淋。 經(jīng)常有香客問我,道長泄朴,這世上最難降的妖魔是什么重抖? 我笑而不...
    開封第一講書人閱讀 59,714評論 1 297
  • 正文 為了忘掉前任,我火速辦了婚禮祖灰,結(jié)果婚禮上钟沛,老公的妹妹穿的比我還像新娘。我一直安慰自己局扶,他們只是感情好恨统,可當(dāng)我...
    茶點故事閱讀 68,724評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著三妈,像睡著了一般畜埋。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上畴蒲,一...
    開封第一講書人閱讀 52,328評論 1 310
  • 那天悠鞍,我揣著相機(jī)與錄音,去河邊找鬼模燥。 笑死咖祭,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的蔫骂。 我是一名探鬼主播么翰,決...
    沈念sama閱讀 40,897評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼纠吴!你這毒婦竟也來了硬鞍?” 一聲冷哼從身側(cè)響起慧瘤,我...
    開封第一講書人閱讀 39,804評論 0 276
  • 序言:老撾萬榮一對情侶失蹤戴已,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后锅减,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體糖儡,經(jīng)...
    沈念sama閱讀 46,345評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,431評論 3 340
  • 正文 我和宋清朗相戀三年怔匣,在試婚紗的時候發(fā)現(xiàn)自己被綠了握联。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片桦沉。...
    茶點故事閱讀 40,561評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖金闽,靈堂內(nèi)的尸體忽然破棺而出纯露,到底是詐尸還是另有隱情,我是刑警寧澤代芜,帶...
    沈念sama閱讀 36,238評論 5 350
  • 正文 年R本政府宣布埠褪,位于F島的核電站,受9級特大地震影響挤庇,放射性物質(zhì)發(fā)生泄漏钞速。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,928評論 3 334
  • 文/蒙蒙 一嫡秕、第九天 我趴在偏房一處隱蔽的房頂上張望渴语。 院中可真熱鬧,春花似錦昆咽、人聲如沸驾凶。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,417評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽狭郑。三九已至,卻和暖如春汇在,著一層夾襖步出監(jiān)牢的瞬間翰萨,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,528評論 1 272
  • 我被黑心中介騙來泰國打工糕殉, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留亩鬼,地道東北人。 一個月前我還...
    沈念sama閱讀 48,983評論 3 376
  • 正文 我出身青樓阿蝶,卻偏偏與公主長得像雳锋,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子羡洁,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,573評論 2 359

推薦閱讀更多精彩內(nèi)容