![](/img/trans.png)
[英]IOS/Objective-C: Detect swipe over UIVIew and button touch up in same location of screen
[英]iOS Detect tap down and touch up of a UIView
我遇到了確定如何檢測 UIView 被觸碰和 UIView 被點擊的問題。 當它觸地時,我希望 UIView 改變它的背景顏色。 當它被觸摸時,我希望 UIView 執行某些任務。 我想知道如何解決這個問題。
-(void)viewDidLoad
{
UITapGestureRecognizer *dismissGestureRecognition = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDismissDoubleTap:)];
dismissGestureRecognition.numberOfTapsRequired = 1;
[sectionDismissDoubleView addGestureRecognizer:dismissGestureRecognition];
UITapGestureRecognizer *dismissGestureDownRecognition = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissGestureDownRecognition:)];
dismissGestureRecognition.numberOfTouchesRequired = 1;
[sectionDismissDoubleView addGestureRecognizer:dismissGestureDownRecognition];
}
- (void)handleDismissDoubleTap:(UIGestureRecognizer*)tap {
SettingsDismissDoubleViewController *settingsDouble = [[SettingsDismissDoubleViewController alloc] initWithNibName:@"SettingsDismissDoubleViewController" bundle:nil];
[self.navigationController pushViewController:settingsDouble animated:YES];
}
- (void)dismissGestureDownRecognition:(UIGestureRecognizer*)tap {
NSLog(@"Down");
}
這個方法不需要子類化任何東西。 您只需將UILongPressGestureRecognizer
添加到視圖並將minimumPressDuration
設置為零。 然后檢查調用手勢事件時的狀態,以查看觸摸事件是開始還是結束。
這是上面示例圖像的整個項目代碼。
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var myView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
let tap = UILongPressGestureRecognizer(target: self, action: #selector(tapHandler))
tap.minimumPressDuration = 0
myView.addGestureRecognizer(tap)
}
@objc func tapHandler(gesture: UITapGestureRecognizer) {
// there are seven possible events which must be handled
if gesture.state == .began {
myView.backgroundColor = UIColor.darkGray
return
}
if gesture.state == .changed {
print("very likely, just that the finger wiggled around while the user was holding down the button. generally, just ignore this")
return
}
if gesture.state == .possible || gesture.state == .recognized {
print("in almost all cases, simply ignore these two, unless you are creating very unusual custom subclasses")
return
}
// the three remaining states are
// .cancelled, .failed, and .ended
// in all three cases, must return to the normal button look:
myView.backgroundColor = UIColor.lightGray
}
}
手勢識別器可能對您想要的東西來說太過分了。 您可能只想使用-touchesBegan:withEvent:
和-touchesEnded:withEvent:
。
這是有缺陷的,但它應該讓你知道你想要做什么。
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
self.touchDown = YES;
self.backgroundColor = [UIColor redColor];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
// Triggered when touch is released
if (self.isTouchDown) {
self.backgroundColor = [UIColor whiteColor];
self.touchDown = NO;
}
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
// Triggered if touch leaves view
if (self.isTouchDown) {
self.backgroundColor = [UIColor whiteColor];
self.touchDown = NO;
}
}
此代碼應該放在您創建的UIView
的自定義子類中。 然后使用此自定義視圖類型而不是UIView
,您將獲得觸摸處理。
在每個 UIControl 子類(UIButton 等)中,您可以使用它來訂閱一組特定的 UIControlEvent:
addTarget:action:forControlEvents
You should add target with appropriate selector for UIControlEventTouchDown and another target/selector for UIControlEventTouchUpInside event.
感謝Holly 的回答,我構建了一個ButtonView
便利類。
編輯:正如這個答案所說, UILongPressGestureRecognizer
反應速度相當快,所以我更新了我的課程。
用法:
let btn = ButtonView()
btn.onNormal = { btn.backgroundColor = .clearColor() }
btn.onPressed = { btn.backgroundColor = .blueColor() }
btn.onReleased = yourAction // Function to be called
班級:
/** View that can be pressed like a button */
import UIKit
class ButtonView : UIView {
/* Called when the view goes to normal state (set desired appearance) */
var onNormal = {}
/* Called when the view goes to pressed state (set desired appearance) */
var onPressed = {}
/* Called when the view is released (perform desired action) */
var onReleased = {}
override init(frame: CGRect)
{
super.init(frame: frame)
let recognizer = UILongPressGestureRecognizer(target: self, action: Selector("touched:"))
recognizer.delegate = self
recognizer.minimumPressDuration = 0.0
addGestureRecognizer(recognizer)
userInteractionEnabled = true
onNormal()
}
func touched(sender: UILongPressGestureRecognizer)
{
if sender.state == .Began {
onPressed(self)
} else if sender.state == .Ended {
onNormal(self)
onReleased()
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
首先,通過您的選擇器handleDismissDoubleTap:
,我假設您正在尋找雙擊以關閉。 為此,您應該這樣做: dismissGestureRecognition.numberOfTapsRequired = 2;
其次,如果觸摸是指長時間點擊(或“點擊並按住”)類型的手勢,則應使用UILongPressGestureRecognizer
而不是UITapGestureRecognizer
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.