我在我的视图中设置了一个UILongPressGestureRecognizer来处理四个不同的按钮,如何在我的代码中访问哪个按钮被点击?
我的UILongPressGestureRecognizer看起来像:
@IBAction func editText(sender: UILongPressGestureRecognizer) {
textFieldInput.hidden = false
iphoneSaveCharName.hidden = false
}
我想使用Long Press以便我可以编辑按钮文本
编辑1:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var iphoneTableView: UITableView!
@IBOutlet weak var textFieldInput: UITextField!
@IBOutlet weak var iphoneSaveCharName: UIButton!
@IBOutlet weak var charOne: UILabel!
@IBOutlet weak var charTwo: UILabel!
@IBOutlet weak var charTree: UILabel!
@IBOutlet weak var charFour: UILabel!
@IBOutlet weak var test1: UIButton! //button that I am clicking on!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//Function I made so I can save the user input
@IBAction func iphoneSaveTextInput(sender: UIButton) {
let textData = textFieldInput.text
textFieldInput.hidden = true
iphoneSaveCharName.hidden = true
charTwo.text = textData
}
// This is the LongPress Action
@IBAction func editText(sender: UILongPressGestureRecognizer) {
textFieldInput.hidden = false
iphoneSaveCharName.hidden = false
func longPressMethod(gesture: UILongPressGestureRecognizer) {
println(gesture.view)
if gesture.view is UIButton {
let test1 = gesture.view as UIButton
println(test1)
}
}
}
}
编辑2:Layout
编辑3:新的ViewController
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var iphoneTableView: UITableView!
@IBOutlet weak var textFieldInput: UITextField!
@IBOutlet weak var iphoneSaveCharName: UIButton!
@IBOutlet weak var charOne: UIButton!
@IBOutlet weak var charTwo: UIButton!
@IBOutlet weak var charThree: UIButton!
@IBOutlet weak var charFour: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func iphoneSaveTextInput(sender: UIButton) -> Void{
let textData = textFieldInput.text
textFieldInput.hidden = true
iphoneSaveCharName.hidden = true
}
@IBAction func editText(sender: AnyObject) {
if sender is UILongPressGestureRecognizer &&
sender.state == UIGestureRecognizerState.Began {
textFieldInput.hidden = false
iphoneSaveCharName.hidden = false
// func iphoneSaveTextInput(sender: UIButton){
// var textData = textFieldInput.text
// textFieldInput.hidden = true
// iphoneSaveCharName.hidden = true
//
// }
let button = sender.view as UIButton
println(button)
if button.tag == 1{
charOne.setTitle("textData", forState: .Normal)
} else if button.tag == 2{
charTwo.setTitle("textData2", forState: .Normal)
} else if button.tag == 3{
charThree.setTitle("textData3", forState: .Normal)
} else if button.tag == 4{
charFour.setTitle("textData4", forState: .Normal)
}
}
}
}
答案:
这是最终的视图控件:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var iphoneTableView: UITableView!
@IBOutlet weak var textFieldInput: UITextField!
@IBOutlet weak var iphoneSaveCharName: UIButton!
@IBOutlet weak var charOne: UIButton!
@IBOutlet weak var charTwo: UIButton!
@IBOutlet weak var charThree: UIButton!
@IBOutlet weak var charFour: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func iphoneSaveTextInput(sender: UIButton) -> Void{
let textData = textFieldInput.text
textFieldInput.hidden = true
iphoneSaveCharName.hidden = true
}
@IBAction func editText(sender: AnyObject) {
if sender is UILongPressGestureRecognizer &&
sender.state == UIGestureRecognizerState.Began {
textFieldInput.hidden = false
iphoneSaveCharName.hidden = false
// func iphoneSaveTextInput(sender: UIButton){
// var textData = textFieldInput.text
// textFieldInput.hidden = true
// iphoneSaveCharName.hidden = true
//
// }
let button = sender.view as UIButton
println(button)
if button.tag == 1{
charOne.setTitle("textData", forState: .Normal)
} else if button.tag == 2{
charTwo.setTitle("textData2", forState: .Normal)
} else if button.tag == 3{
charThree.setTitle("textData3", forState: .Normal)
} else if button.tag == 4{
charFour.setTitle("textData4", forState: .Normal)
}
}
}
}
我很想给你一个最好的答案,因为每个人都帮助了我!我不得不为每个按钮创建一个长按,否则代码会混淆。
答案 0 :(得分:5)
您的问题向我介绍了一个非常酷的功能,谢谢! :)
事实证明,如果您将UILongPressGestureRecognizer
附加到故事板中的UIButton
并将该按钮和手势附加到您的快速课程中的IBAction
,IBAction
方法可以识别触摸是点击还是长按!很酷,嗯?
首先,请确保每个UIButton
都有自己唯一的UILongPressGestureRecognizer
;那么你可以像这样编辑你的代码,这样它就可以识别出哪个按钮被按下,以及该按下是简单点击还是UILongPressGestureRecognizer
:
// Connect both your button *and* its gestures to the
// `IBAction` method so that the function will be called
// no matter what kind of gesture it recognizes -- tap,
// long press, or otherwise.
@IBAction func buttonSelected(sender: AnyObject) {
// But to see if the gesture is a long press, you can
// simply check the sender's class and execute the code
// when the gesture begins.
if sender is UILongPressGestureRecognizer &&
sender.state == UIGestureRecognizerState.Began {
// These two lines are originally from your
// editText method
textFieldInput.hidden = false
iphoneSaveCharName.hidden = false
// Then to identify which button was long pressed you
// can check the sender's view, to see which button IBOutlet
// that gesture's view belongs to.
// Method #1:
let button = sender.view as UIButton
if button == thisButton {
} else if button == thatButton {
}
...
// Or you can check the gesture view's tag to see which
// button it belongs to (i.e. whichever button has a matching
// tag).
// Method #2:
let button = sender.view as UIButton
if button.tag == 1 {
} else if button.tag == 2 {
}
...
}
// Else if it's not a long press gesture, perform
// whatever action you'd like to accomplish during a
// normal button tap
else if !(sender is UILongPressGestureRecognizer) {
// These lines are originally from your
// iphoneSaveTextInput
let textData = textFieldInput.text
textFieldInput.hidden = true
iphoneSaveCharName.hidden = true
}
}
但是,如果您希望继续将UILongPressGestureRecognizer
IBAction
与UIButton
的{{1}}分开(即将IBAction
链接到{{ 1}}和UIButton
到iphoneSaveTextInput:
),也应该没问题。只需按原样保留UILongPressGestureRecognizer
方法,然后更新editText:
方法:
iphoneSaveTextInput:
答案 1 :(得分:0)
您不希望在不同的视图中使用相同的手势识别器:
Can you attach a UIGestureRecognizer to multiple views?
手势识别器的view
属性可能是您想要检查的属性,但您希望每个视图都有不同的手势识别器。
如果你愿意,它们都可以链接到同一个选择器,你可以通过这种方式访问传入识别器的视图。
答案 2 :(得分:0)
制作按钮的一般方法根据您按下它的时间而有不同的动作,看起来像这样(计时器是属性),
@IBAction func buttonDown(sender: UIButton) { // connected to touchDown
timer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: "handleTimer", userInfo: sender, repeats: false)
}
@IBAction func buttonUp(sender: UIButton) { // connected to touchUpInside
if timer != nil {
timer.invalidate()
println("change view")
}
}
func handleTimer() {
var button = timer.userInfo as UIButton
timer = nil
button.setTitle("New Title", forState: .Normal)
}