How can I add UIView touchbegin action or touchend action programmatically as Xcode is not providing from Main.storyboard?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
You will have to add it through code. Try this:
// 1.create UIView programmetically
var myView = UIView(frame: CGRectMake(100, 100, 100, 100))
// 2.add myView to UIView hierarchy
self.view.addSubview(myView)
// 3. add action to myView
let gesture = UITapGestureRecognizer(target: self, action: "someAction:")
// or for swift 2 +
let gestureSwift2AndHigher = UITapGestureRecognizer(target: self, action: #selector (self.someAction (_:)))
self.myView.addGestureRecognizer(gesture)
func someAction(sender:UITapGestureRecognizer){
// do other task
}
// or for Swift 3
func someAction(_ sender:UITapGestureRecognizer){
// do other task
}
// or for Swift 4
@objc func someAction(_ sender:UITapGestureRecognizer){
// do other task
}
回答2:
Update for Swift 4:
let gesture = UITapGestureRecognizer(target: self, action: #selector(self.checkAction))
self.myView.addGestureRecognizer(gesture)
@objc func checkAction(sender : UITapGestureRecognizer) {
// Do what you want
}
Update for Swift 3:
let gesture = UITapGestureRecognizer(target: self, action: #selector(self.checkAction(sender:)))
self.myView.addGestureRecognizer(gesture)
func checkAction(sender : UITapGestureRecognizer) {
// Do what you want
}
回答3:
Updating @Crashalot's answer for Swift 3.x:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
let currentPoint = touch.location(in: self)
// do something with your currentPoint
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
let currentPoint = touch.location(in: self)
// do something with your currentPoint
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
let currentPoint = touch.location(in: self)
// do something with your currentPoint
}
}
回答4:
Updating @Chackle's answer for Swift 2.x:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let touch = touches.first {
let currentPoint = touch.locationInView(self)
// do something with your currentPoint
}
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let touch = touches.first {
let currentPoint = touch.locationInView(self)
// do something with your currentPoint
}
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let touch = touches.first {
let currentPoint = touch.locationInView(self)
// do something with your currentPoint
}
}
回答5:
Put this in your UIView
subclass (it's easiest if you make a sublcass for this functionality).
class YourView: UIView {
//Define your initialisers here
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
if let touch = touches.first as? UITouch {
let currentPoint = touch.locationInView(self)
// do something with your currentPoint
}
}
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
if let touch = touches.first as? UITouch {
let currentPoint = touch.locationInView(self)
// do something with your currentPoint
}
}
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
if let touch = touches.first as? UITouch {
let currentPoint = touch.locationInView(self)
// do something with your currentPoint
}
}
}
回答6:
For swift 4
@IBOutlet weak var someView: UIView!
let gesture = UITapGestureRecognizer(target: self, action: #selector (self.someAction (_:)))
self.someView.addGestureRecognizer(gesture)
@objc func someAction(_ sender:UITapGestureRecognizer){
print("view was clicked")
}
回答7:
Just an update to above answers :
If you want to have see changes in the click event, i.e. Color of your UIVIew shud change whenever user clicks the UIView then make changes as below...
class ClickableUIView: UIView {
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let touch = touches.first {
let currentPoint = touch.locationInView(self)
// do something with your currentPoint
}
self.backgroundColor = UIColor.magentaColor()//Color when UIView is clicked.
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let touch = touches.first {
let currentPoint = touch.locationInView(self)
// do something with your currentPoint
}
self.backgroundColor = UIColor.magentaColor()//Color when UIView is clicked.
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let touch = touches.first {
let currentPoint = touch.locationInView(self)
// do something with your currentPoint
}
self.backgroundColor = UIColor.whiteColor()//Color when UIView is not clicked.
}//class closes here
Also, call this Class from Storyboard & ViewController as:
@IBOutlet weak var panVerificationUIView:ClickableUIView!
回答8:
Updating @stevo.mit's answer for Swift 4.x:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
let currentPoint = touch.location(in: self.view)
// do something with your currentPoint
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
let currentPoint = touch.location(in: self.view)
// do something with your currentPoint
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
let currentPoint = touch.location(in: self.view)
// do something with your currentPoint
}
}
回答9:
you can used this way: create extension
extension UIView {
func addTapGesture(action : @escaping ()->Void ){
let tap = MyTapGestureRecognizer(target: self , action: #selector(self.handleTap(_:)))
tap.action = action
tap.numberOfTapsRequired = 1
self.addGestureRecognizer(tap)
self.isUserInteractionEnabled = true
}
@objc func handleTap(_ sender: MyTapGestureRecognizer) {
sender.action!()
}
}
class MyTapGestureRecognizer: UITapGestureRecognizer {
var action : (()->Void)? = nil
}
and use this way:
@IBOutlet weak var testView: UIView!
testView.addTapGesture{
// ...
}
回答10:
Swift 4.2:
@IBOutlet weak var viewLabel1: UIView!
@IBOutlet weak var viewLabel2: UIView!
override func viewDidLoad() {
super.viewDidLoad()
let myView = UITapGestureRecognizer(target: self, action: #selector(someAction(_:)))
self.viewLabel1.addGestureRecognizer(myView)
}
@objc func someAction(_ sender:UITapGestureRecognizer){
viewLabel2.isHidden = true
}