日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

UIButton拖动响应事件,距离问题

發布時間:2025/4/5 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 UIButton拖动响应事件,距离问题 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

問題描述:

最近開發遇到個問題,就是UIButton響應了UIContolEventTouchDragEnter、UIControlEventTouchDragExit事件后,在拖出按鈕的時候不會立即響應事件,而是真正超出70px的才會響應。

猜測蘋果這樣做的原因是估計到小按鈕手指點擊容易有抖動,會不小心有移出按鈕的誤操作。

效果是這樣的:(盜個圖)

解決辦法:

響應UIContolEventTouchDragInside、UIControlEventTouchDragOutSide方法(大家會問和enter、exit的區別)

UIControlEventTouchDragInside 當一次觸摸在控件窗口內拖動時。 UIControlEventTouchDragOutside 當一次觸摸在控件窗口之外拖動時。 UIControlEventTouchDragEnter 當一次觸摸從控件窗口之外拖動到內部時。 UIControlEventTouchDragExit 當一次觸摸從控件窗口內部拖動到外部時。

響應到一個函數里:

[btn?addTarget:self?action:@selector(sendButtonDrag:withEvent:)?forControlEvents:UIControlEventTouchDragInside];? [btn?addTarget:self?action:@selector(sendButtonDrag:withEvent:)?forControlEvents:UIControlEventTouchDragOutside]; -?(IBAction)sendButtonDrag:(UIButton?*)sender?withEvent:(UIEvent?*)event?{if?([self?isInButtonBounds:sender?event:event])?{//?在里面}?else?{//?在外面} }-?(BOOL)isInButtonBounds:(UIButton?*)button?event:(UIEvent?*)event?{UITouch?*touch?=?[[event?allTouches]?anyObject];CGFloat?boundsExtension?=?5.0f;?//擴展范圍閥值CGRect?outerBounds?=?CGRectInset(button.bounds,?-1?*?boundsExtension,?-1?*?boundsExtension);BOOL?touchOutside?=?!CGRectContainsPoint(outerBounds,?[touch?locationInView:button]);if?(touchOutside)?{BOOL?previewTouchInside?=?CGRectContainsPoint(outerBounds,?[touch?previousLocationInView:button]);if?(previewTouchInside)?{//?UIControlEventTouchDragExit}?else?{//?UIControlEventTouchDragOutside}return?NO;}?else?{BOOL?previewTouchOutside?=?!CGRectContainsPoint(outerBounds,?[touch?previousLocationInView:button]);if?(previewTouchOutside)?{//?UIControlEventTouchDragEnter}?else?{//?UIControlEventTouchDragInside}return?YES;} }

當然也要在 UIControlEventTouchUpInside的響應函數里做上面判斷,不然的話會顯示離開,但是抬手還是會響應點擊函數的。

-?(IBAction)sendAudioRecordButton:(id)sender?withEvent:(UIEvent?*)event?{if?([self?isInButtonBounds:sender?event:event])?{//?點擊按鈕} }


轉載于:https://my.oschina.net/iq19900204/blog/525261

總結

以上是生活随笔為你收集整理的UIButton拖动响应事件,距离问题的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。