iOS UIGestureRecognizer(手勢操作)

//聯(lián)系人:石虎QQ: 1224614774昵稱:嗡嘛呢叭咪哄

#import"ViewController.h"

@interfaceViewController ()

/**圖片*/

@property(nonatomic,weak)IBOutletUIImageView *imageView;

@end

@implementationViewController

- (void)viewDidLoad {

[superviewDidLoad];

self.imageView.userInteractionEnabled =YES;

[selfsetUpTap];

[selfsetUpLongPress];

[selfsetUpSwipe];

[selfsetUpRotation];

[selfsetPinch];

[selfsetPan];

}

/*------------長按手勢----------------*/

- (void)setUpTap {

//創(chuàng)建點擊事件

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:selfaction:@selector(tap)];

tap.delegate =self;

[self.imageView addGestureRecognizer:tap];

}

//點擊事件

- (void)tap {

NSLog(@"%s",__func__);

}

/*------------長按手勢----------------*/

- (void)setUpLongPress {

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:selfaction:@selector(longPress:)];

longPress.delegate =self;

[self.imageView addGestureRecognizer:longPress];

}

//長按手勢事件

- (void)longPress:(UILongPressGestureRecognizer *)sender {

//注意:長安手勢可以觸發(fā)兩次事件娜睛,我們需要判斷手勢狀態(tài)唤崭,如下

if(sender.state == UIGestureRecognizerStateEnded) {

NSLog(@"%s",__func__);

}

}

/*------------輕掃手勢----------------*/

- (void)setUpSwipe {

UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:selfaction:@selector(swipe:)];

swipe.delegate =self;

//注意:如果以后想要一個控件支持多個方向的清掃忙灼,必須創(chuàng)建多個清掃手勢惰蜜,一個清掃手勢只能支持一個方向

//默認清掃手勢的方向是向右

swipe.direction = UISwipeGestureRecognizerDirectionRight;

[self.imageView addGestureRecognizer:swipe];

}

//輕掃手勢事件

- (void)swipe:(UISwipeGestureRecognizer *)sender {

NSLog(@"%s",__func__);

}

/*------------旋轉手勢----------------*/

- (void)setUpRotation {

UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:selfaction:@selector(rotation:)];

rotation.delegate =self;

[self.imageView addGestureRecognizer:rotation];

}

//旋轉手勢事件

- (void)rotation:(UIRotationGestureRecognizer *)sender {

//注意:手勢傳遞的旋轉角度都是相對于最開始的位置

CGFloat rotation = sender.rotation;

self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, rotation);

//復位

sender.rotation =0;

NSLog(@"%s",__func__);

}

/*------------捏合手勢----------------*/

- (void)setPinch {

UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:selfaction:@selector(pinch:)];

pinch.delegate =self;

[self.imageView addGestureRecognizer:pinch];

}

//捏合手勢事件

- (void)pinch:(UIPinchGestureRecognizer *)sender {

CGFloat scale = sender.scale;

self.imageView.transform = CGAffineTransformScale(self.imageView.transform, scale, scale);

//復位

sender.scale =1.0;

NSLog(@"%s",__func__);

}

/*------------拖拽手勢----------------*/

- (void)setPan {

UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:selfaction:@selector(pan:)];

pan.delegate =self;

[self.imageView addGestureRecognizer:pan];

}

//拖拽手勢事件

- (void)pan:(UIPanGestureRecognizer *)sender {

//獲取手勢的移動调俘,也是相對于最開始的位置

CGPoint transP = [sender translationInView:self.imageView];

self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, transP.x, transP.y);

//復位

[sender setTranslation:CGPointZero inView:self.imageView];

NSLog(@"%s",__func__);

}

#pragma mark -

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {

returnYES;

}

//是否允許同時支持多個手勢,默認不支持多個手勢

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {

returnYES;

}

//是否可以接收手指的觸摸點(touch)

//實例:該方法可以控制讓一個視圖挡闰,左邊能點擊头岔,右邊不能點擊

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {

CGPoint currentPoint = [touch locationInView:self.imageView];

if(currentPoint.x >self.imageView.bounds.size.width *0.5) {

returnYES;

}

returnNO;

}

@end

謝謝!!!

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市劲赠,隨后出現的幾起案子涛目,更是在濱河造成了極大的恐慌,老刑警劉巖凛澎,帶你破解...
    沈念sama閱讀 222,627評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件霹肝,死亡現場離奇詭異,居然都是意外死亡塑煎,警方通過查閱死者的電腦和手機沫换,發(fā)現死者居然都...
    沈念sama閱讀 95,180評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來最铁,“玉大人讯赏,你說我怎么就攤上這事±湮荆” “怎么了漱挎?”我有些...
    開封第一講書人閱讀 169,346評論 0 362
  • 文/不壞的土叔 我叫張陵,是天一觀的道長网严。 經常有香客問我识樱,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 60,097評論 1 300
  • 正文 為了忘掉前任怜庸,我火速辦了婚禮当犯,結果婚禮上,老公的妹妹穿的比我還像新娘割疾。我一直安慰自己嚎卫,他們只是感情好,可當我...
    茶點故事閱讀 69,100評論 6 398
  • 文/花漫 我一把揭開白布宏榕。 她就那樣靜靜地躺著拓诸,像睡著了一般。 火紅的嫁衣襯著肌膚如雪麻昼。 梳的紋絲不亂的頭發(fā)上奠支,一...
    開封第一講書人閱讀 52,696評論 1 312
  • 那天,我揣著相機與錄音抚芦,去河邊找鬼倍谜。 笑死,一個胖子當著我的面吹牛叉抡,可吹牛的內容都是我干的尔崔。 我是一名探鬼主播,決...
    沈念sama閱讀 41,165評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼褥民,長吁一口氣:“原來是場噩夢啊……” “哼季春!你這毒婦竟也來了?” 一聲冷哼從身側響起消返,我...
    開封第一講書人閱讀 40,108評論 0 277
  • 序言:老撾萬榮一對情侶失蹤载弄,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后侦副,有當地人在樹林里發(fā)現了一具尸體侦锯,經...
    沈念sama閱讀 46,646評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 38,709評論 3 342
  • 正文 我和宋清朗相戀三年秦驯,在試婚紗的時候發(fā)現自己被綠了尺碰。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,861評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡译隘,死狀恐怖亲桥,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情固耘,我是刑警寧澤题篷,帶...
    沈念sama閱讀 36,527評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站厅目,受9級特大地震影響番枚,放射性物質發(fā)生泄漏法严。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 42,196評論 3 336
  • 文/蒙蒙 一葫笼、第九天 我趴在偏房一處隱蔽的房頂上張望深啤。 院中可真熱鬧,春花似錦路星、人聲如沸溯街。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,698評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽呈昔。三九已至,卻和暖如春友绝,著一層夾襖步出監(jiān)牢的瞬間堤尾,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,804評論 1 274
  • 我被黑心中介騙來泰國打工九榔, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留哀峻,地道東北人涡相。 一個月前我還...
    沈念sama閱讀 49,287評論 3 379
  • 正文 我出身青樓哲泊,卻偏偏與公主長得像,于是被迫代替她去往敵國和親催蝗。 傳聞我的和親對象是個殘疾皇子切威,可洞房花燭夜當晚...
    茶點故事閱讀 45,860評論 2 361

推薦閱讀更多精彩內容