2011-01-11 80 views
237

如何將觸摸事件添加到UIView?
我嘗試:如何將觸摸事件添加到UIView?

UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, nextY)] autorelease]; 
[headerView addTarget:self action:@selector(myEvent:) forControlEvents:UIControlEventTouchDown]; 
// ERROR MESSAGE: UIView may not respond to '-addTarget:action:forControlEvents:' 

我不想創建一個子類,並覆蓋

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
+0

爲SWIFT你可以從這裏得到它:https://stackoverflow.com/questions/28675209/how-to-call-gesture-tap-on -uiview-programmatically-in-swift – 2018-03-07 13:45:06

回答

521

在iOS 3.2或更高版本,可以使用手勢識別。例如,您可以如何處理點按事件:

//The setup code (in viewDidLoad in your view controller) 
UITapGestureRecognizer *singleFingerTap = 
    [[UITapGestureRecognizer alloc] initWithTarget:self 
              action:@selector(handleSingleTap:)]; 
[self.view addGestureRecognizer:singleFingerTap]; 

//The event handling method 
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer 
{ 
    CGPoint location = [recognizer locationInView:[recognizer.view superview]]; 

    //Do stuff here... 
} 

還有一些內置的手勢。查看iOS事件處理文檔和UIGestureRecognizer。我還在github上有一堆示例代碼,可能有所幫助。

+0

但是,這是重寫視圖的觸摸defautlt動作。 ¿是否可以調用視圖的默認觸摸操作?像[super touchesbegan]之類的東西?... – 2011-07-13 15:53:35

+2

CGPoint線做了什麼? – zakdances 2012-05-13 02:11:59

+2

@yourfriendzak「CGPoint」表示點擊視圖的超級視圖中點擊的位置。您可以使用此點將輕敲視圖(或同級視圖)移至輕敲的位置。這在'UIPanGestureRecognizer`的處理程序中用於在屏幕上拖動視圖更加有用。 – 2012-05-15 17:10:02

50

我想你可以簡單地使用

UIControl *headerView = ... 
[headerView addTarget:self action:@selector(myEvent:) forControlEvents:UIControlEventTouchDown]; 

我的意思是headerView從UIControl延伸。

16

基礎上accepted answer你可以定義一個宏:

#define handle_tap(view, delegate, selector) do {\ 
    view.userInteractionEnabled = YES;\ 
    [view addGestureRecognizer: [[UITapGestureRecognizer alloc] initWithTarget:delegate action:selector]];\ 
} while(0) 

本宏使用ARC,所以沒有release電話。

宏的用法例如:

handle_tap(userpic, self, @selector(onTapUserpic:)); 
9

您可以通過在代碼中添加手勢識別器實現這一目標。

第1步: ViewController.m:

// Declare the Gesture. 
UITapGestureRecognizer *gesRecognizer = [[UITapGestureRecognizer alloc] 
              initWithTarget:self 
              action:@selector(handleTap:)]; 
gesRecognizer.delegate = self; 

// Add Gesture to your view. 
[yourView addGestureRecognizer:gesRecognizer]; 

第2步: ViewController.m:

// Declare the Gesture Recogniser handler method. 
- (void)handleTap:(UITapGestureRecognizer *)gestureRecognizer{ 
    NSLog(@"Tapped"); 
} 

注:此處yourView在我的情況是@property (strong, nonatomic) IBOutlet UIView *localView;

編輯: * localView是白框我ÑMain.storyboard從下面

enter image description here

enter image description here

5

下面有一個夫特版本:

// MARK: Gesture Extensions 
extension UIView { 

    func addTapGesture(#tapNumber: Int, target: AnyObject, action: Selector) { 
     let tap = UITapGestureRecognizer (target: target, action: action) 
     tap.numberOfTapsRequired = tapNumber 
     addGestureRecognizer(tap) 
     userInteractionEnabled = true 
    } 

    func addTapGesture(#tapNumber: Int, action: ((UITapGestureRecognizer)->())?) { 
     let tap = BlockTap (tapCount: tapNumber, fingerCount: 1, action: action) 
     addGestureRecognizer(tap) 
     userInteractionEnabled = true 
    } 
} 
79

姿勢識別

有許多常用的觸摸事件(或姿勢),您可以通知您何時將Gesture Recognizer添加到您的視圖中。他們下面的手勢類型的默認支持:

  • UITapGestureRecognizer點擊(觸摸屏幕簡要一次或多次)
  • UILongPressGestureRecognizer長按(接觸時間長了屏幕)
  • UIPanGestureRecognizer(移動你的手指在屏幕上)
  • UISwipeGestureRecognizer刷卡(移動迅速手指)
  • UIPinchGestureRecognizer(移動兩個手指在一起或分開 - 通常進行縮放)
  • UIRotationGestureRecognizer旋轉

(在圓形方向上移動兩個手指)除這些,你也可以製作你自己的自定義手勢識別器。

添加手勢在界面生成器

拖動從對象庫手勢識別到你的觀點。從文檔大綱到您的視圖控制器代碼手勢

enter image description here

調速拖動,以使出口和動作。

enter image description here

添加手勢編程

以編程方式添加一個手勢,則(1)創建手勢識別,(2)將其添加到圖,(3)使該是這樣的方法當手勢被識別時調用。

import UIKit 
class ViewController: UIViewController { 

    @IBOutlet weak var myView: UIView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     // 1. create a gesture recognizer (tap gesture) 
     let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap(sender:))) 

     // 2. add the gesture recognizer to a view 
     myView.addGestureRecognizer(tapGesture) 
    } 

    // 3. this method is called when a tap is recognized 
    func handleTap(sender: UITapGestureRecognizer) { 
     print("tap") 
    } 
} 

  • sender參數是可選的。如果你不需要對手勢的引用,那麼你可以將其忽略掉。但是,如果您這樣做,請在創建手勢時在操作方法名稱後面刪除(_:)
  • handleTap方法的命名是任意的。使用action: #selector(someMethodName(sender:))命名它。

更多示例

你可以學習,我加入到這些意見,看看他們是如何工作的手勢識別。

enter image description here

下面是該項目的代碼:

import UIKit 
class ViewController: UIViewController { 

    @IBOutlet weak var tapView: UIView! 
    @IBOutlet weak var doubleTapView: UIView! 
    @IBOutlet weak var longPressView: UIView! 
    @IBOutlet weak var panView: UIView! 
    @IBOutlet weak var swipeView: UIView! 
    @IBOutlet weak var pinchView: UIView! 
    @IBOutlet weak var rotateView: UIView! 
    @IBOutlet weak var label: UILabel! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Tap 
     let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap)) 
     tapView.addGestureRecognizer(tapGesture) 

     // Double Tap 
     let doubleTapGesture = UITapGestureRecognizer(target: self, action: #selector(handleDoubleTap)) 
     doubleTapGesture.numberOfTapsRequired = 2 
     doubleTapView.addGestureRecognizer(doubleTapGesture) 

     // Long Press 
     let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress(gesture:))) 
     longPressView.addGestureRecognizer(longPressGesture) 

     // Pan 
     let panGesture = UIPanGestureRecognizer(target: self, action: #selector(handlePan(gesture:))) 
     panView.addGestureRecognizer(panGesture) 

     // Swipe (right and left) 
     let swipeRightGesture = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipe(gesture:))) 
     let swipeLeftGesture = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipe(gesture:))) 
     swipeRightGesture.direction = UISwipeGestureRecognizerDirection.right 
     swipeLeftGesture.direction = UISwipeGestureRecognizerDirection.left 
     swipeView.addGestureRecognizer(swipeRightGesture) 
     swipeView.addGestureRecognizer(swipeLeftGesture) 

     // Pinch 
     let pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(handlePinch(gesture:))) 
     pinchView.addGestureRecognizer(pinchGesture) 

     // Rotate 
     let rotateGesture = UIRotationGestureRecognizer(target: self, action: #selector(handleRotate(gesture:))) 
     rotateView.addGestureRecognizer(rotateGesture) 

    } 

    // Tap action 
    func handleTap() { 
     label.text = "Tap recognized" 

     // example task: change background color 
     if tapView.backgroundColor == UIColor.blue { 
      tapView.backgroundColor = UIColor.red 
     } else { 
      tapView.backgroundColor = UIColor.blue 
     } 

    } 

    // Double tap action 
    func handleDoubleTap() { 
     label.text = "Double tap recognized" 

     // example task: change background color 
     if doubleTapView.backgroundColor == UIColor.yellow { 
      doubleTapView.backgroundColor = UIColor.green 
     } else { 
      doubleTapView.backgroundColor = UIColor.yellow 
     } 
    } 

    // Long press action 
    func handleLongPress(gesture: UILongPressGestureRecognizer) { 
     label.text = "Long press recognized" 

     // example task: show an alert 
     if gesture.state == UIGestureRecognizerState.began { 
      let alert = UIAlertController(title: "Long Press", message: "Can I help you?", preferredStyle: UIAlertControllerStyle.alert) 
      alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil)) 
      self.present(alert, animated: true, completion: nil) 
     } 
    } 

    // Pan action 
    func handlePan(gesture: UIPanGestureRecognizer) { 
     label.text = "Pan recognized" 

     // example task: drag view 
     let location = gesture.location(in: view) // root view 
     panView.center = location 
    } 

    // Swipe action 
    func handleSwipe(gesture: UISwipeGestureRecognizer) { 
     label.text = "Swipe recognized" 

     // example task: animate view off screen 
     let originalLocation = swipeView.center 
     if gesture.direction == UISwipeGestureRecognizerDirection.right { 
      UIView.animate(withDuration: 0.5, animations: { 
       self.swipeView.center.x += self.view.bounds.width 
       }, completion: { (value: Bool) in 
        self.swipeView.center = originalLocation 
      }) 
     } else if gesture.direction == UISwipeGestureRecognizerDirection.left { 
      UIView.animate(withDuration: 0.5, animations: { 
       self.swipeView.center.x -= self.view.bounds.width 
       }, completion: { (value: Bool) in 
        self.swipeView.center = originalLocation 
      }) 
     } 
    } 

    // Pinch action 
    func handlePinch(gesture: UIPinchGestureRecognizer) { 
     label.text = "Pinch recognized" 

     if gesture.state == UIGestureRecognizerState.changed { 
      let transform = CGAffineTransform(scaleX: gesture.scale, y: gesture.scale) 
      pinchView.transform = transform 
     } 
    } 

    // Rotate action 
    func handleRotate(gesture: UIRotationGestureRecognizer) { 
     label.text = "Rotate recognized" 

     if gesture.state == UIGestureRecognizerState.changed { 
      let transform = CGAffineTransform(rotationAngle: gesture.rotation) 
      rotateView.transform = transform 
     } 
    } 
} 

注意

  • 您可以將多個手勢識別器添加到一個單一視圖。但爲了簡單起見,我沒有這樣做(除了滑動手勢)。如果你需要爲你的項目,你應該閱讀gesture recognizer documentation。這是相當可以理解和有幫助的。
  • 我上面的示例已知問題:(1)平移視圖在下一個手勢事件時重置其框架。 (2)第一次滑動時,滑動視圖來自錯誤的方向。 (這些錯誤在我的例子不應該影響你的手勢識別器是如何工作的理解,雖然)

更新了斯威夫特3

0

這裏是IOS tapgesture; 首先,你需要創建行動GestureRecognizer後寫的作用下,下面的代碼如下所示

- (IBAction)tapgesture:(id)sender 

{ 


[_password resignFirstResponder]; 


[_username resignFirstResponder]; 

NSLog(@" TapGestureRecognizer tapped"); 

} 
0

另一種方式是將一個透明的按鈕,查看

UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom]; 
b.frame = CGRectMake(0, 0, headerView.width, headerView.height); 
[headerView addSubview:b]; 
[b addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchDown]; 

然後,處理點擊:

- (void)buttonClicked:(id)sender 
{} 
4

斯威夫特3更新

import UIKit 

extension UIView { 

func addTapGesture(tapNumber : Int, target: Any , action : Selector) { 

    let tap = UITapGestureRecognizer(target: target, action: action) 
    tap.numberOfTapsRequired = tapNumber 
    addGestureRecognizer(tap) 
    isUserInteractionEnabled = true 

    } 
} 

使用

yourView.addTapGesture(tapNumber: 1, target: self, action: #selector(yourMethod)) 
0

創建一個手勢識別(子類),將實現觸摸事件,就像touchesBegan。之後您可以將其添加到視圖中。

這樣你將使用組合而不是子類化(這是請求)。

4

斯威夫特3:

let tapGestureRecognizer: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTapGestureRecognizer(_:))) 
view.addGestureRecognizer(tapGestureRecognizer) 

func handleTapGestureRecognizer(_ gestureRecognizer: UITapGestureRecognizer) { 

} 
0

爲什麼你們不試試SSEventListener

您不需要創建任何手勢識別器,並將您的邏輯與另一種方法區分開來。 SSEventListener支持在視圖上設置監聽器模塊以監聽單擊手勢,雙擊手勢和N輕擊手勢(如果您喜歡),並長按手勢。設置一個水龍頭手勢聽衆變成這樣:

[view ss_addTapViewEventListener:^(UITapGestureRecognizer *recognizer) { ... } numberOfTapsRequired:1];