由于Main.storyboard不提供Xcode,如何以编程方式添加UIView touchbegin动作或touchend动作?
由于Main.storyboard不提供Xcode,如何以编程方式添加UIView touchbegin动作或touchend动作?
Answers:
您将不得不通过代码添加它。试试这个:
// 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
}
// update for Swift UI
Text("Tap me!")
.tapAction {
print("Tapped!")
}
迅捷4/5:
let gesture = UITapGestureRecognizer(target: self, action: #selector(self.checkAction))
self.myView.addGestureRecognizer(gesture)
@objc func checkAction(sender : UITapGestureRecognizer) {
// Do what you want
}
斯威夫特3:
let gesture = UITapGestureRecognizer(target: self, action: #selector(self.checkAction(sender:)))
self.myView.addGestureRecognizer(gesture)
func checkAction(sender : UITapGestureRecognizer) {
// Do what you want
}
为Swift 3.x更新@Crashalot的答案:
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
}
}
为Swift 2.x更新@Chackle的答案:
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
}
}
将其放在您的UIView
子类中(如果为此功能创建子类,这是最简单的)。
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
}
}
}
UIView
您要按哪个按键还是要处理每个按键中的某些按键UIView
?
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
}
从在StoryBoard中创建的视图中创建出口。
@IBOutlet weak var redView: UIView!
@IBOutlet weak var orangeView: UIView!
@IBOutlet weak var greenView: UIView!
覆盖touchesBegan方法。有2种选择,每个人都可以确定哪一种对他更好。
在特殊视图中检测触摸。
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
if touch.view == self.redView {
tapOnredViewTapped()
} else if touch.view == self.orangeView {
orangeViewTapped()
} else if touch.view == self.greenView {
greenViewTapped()
} else {
return
}
}
}
在特殊视图上检测触摸点。
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
let location = touch.location(in: view)
if redView.frame.contains(location) {
redViewTapped()
} else if orangeView.frame.contains(location) {
orangeViewTapped()
} else if greenView.frame.contains(location) {
greenViewTapped()
}
}
}
最后,您需要根据用户单击的视图来声明将要调用的函数。
func redViewTapped() {
print("redViewTapped")
}
func orangeViewTapped() {
print("orangeViewTapped")
}
func greenViewTapped() {
print("greenViewTapped")
}
您可以使用这种方式:创建扩展
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
}
并使用这种方式:
@IBOutlet weak var testView: UIView!
testView.addTapGesture{
// ...
}
只是以上答案的更新:
如果您想查看click事件的变化,即,每当用户单击UIView然后进行如下更改时,UIVIew shud的颜色都会发生变化...
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
另外,从Storyboard和ViewController将该类称为:
@IBOutlet weak var panVerificationUIView:ClickableUIView!
为Swift 4.x更新@ stevo.mit的答案:
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
}
}