????????去年做過一個畫圖軟件卓舵,需要在手機(jī)上進(jìn)行繪制草圖相關(guān)操作熏版。其中一個就是長按進(jìn)行繪制線段矩形等圖形,在此拿出來和大家分享一下氯夷。
1臣樱、創(chuàng)建一個長按手勢,并添加到相應(yīng)view上
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longClick:)];
longPress.minimumPressDuration=0.2;
[recordImageViewaddGestureRecognizer:longPress];
2腮考、創(chuàng)建相應(yīng)的長按觸發(fā)方法(不創(chuàng)建的話雇毫,長按的時候程序會crash)
- (void)longClick:(UILongPressGestureRecognizer *)longPress
{
? ? EM
? ? UIImageView *recordImageView = (UIImageView *)longPress.view;
? ? switch (longPress.state) {
? ? ? ? case UIGestureRecognizerStateBegan:
? ? ? ? {
? ? ? ? ? ? //長按開始
? ? ? ? ? ? recordImageView.transform = CGAffineTransformMakeScale(1.15, 1.15);
? ? ? ? ? ? [em startRecordWith:em.filePath];
? ? ? ? ? ? [recordImageView startAnimating];
? ? ? ? ? ? break;
? ? ? ? }
? ? ? ? case UIGestureRecognizerStateChanged:
? ? ? ? {
? ? ? ? ? ? //移動中
? ? break;
? ? ? ? }
? ? ? ? case UIGestureRecognizerStateEnded:
? ? ? ? {
? ? ? ? ? ? //長按結(jié)束
? ? ? ? ? ? recordImageView.transform = CGAffineTransformMakeScale(1.0, 1.0);
? ? ? ? ? ? [em stopRecordWith:em.filePath];
? ? ? ? ? ? [recordImageView stopAnimating];
? ? ? ? ? ? break;
? ? ? ? }
? ? ? ? default:
? ? ? ? ? ? break;
? ? }
}
????????長按當(dāng)然還有其他的狀態(tài),但是平時開發(fā)中基本上就用到這3種狀態(tài)踩蔚。需要了解具體的棚放,就去蘋果的官方文檔中了解吧。小編水平有限馅闽,如有疏漏之處請?jiān)谙旅媪粞云牵視撔牟杉{您的意見。
ps:這里說一下switch福也,每個case中一定break局骤,不然還會走下面的狀態(tài),小編經(jīng)常會犯這種錯