そもそものやりたいことですがボタンの位置をボタンが配置してあるUIViewControllerで取得したいということですか?
それでしたら以下が参考になるかもしれません。
swift Button タップ スワイプ
推測ですが、コードを書いてみました。
ボタンをサブクラス化してそれをTravelSelectionというUIViewControllerに乗せました。
タップでボタンの座標が表示されます。
swift
1import UIKit
2
3class TravelSelection: UIViewController {
4
5 let button = CustomButton(type: .Custom)
6
7 override func viewDidLoad() {
8 super.viewDidLoad()
9
10 // ボタンを生成
11 button.frame = CGRectMake(100, 100, 200, 100)
12 button.setTitle("Button", forState: .Normal)
13 button.setTitleColor(UIColor.blueColor(), forState: .Normal)
14 button.backgroundColor = UIColor.yellowColor()
15 button.addTarget(self, action: #selector(TravelSelection.buttonPressed(_:)), forControlEvents: .TouchUpInside)
16 button.userInteractionEnabled = true
17 view.addSubview(button)
18 }
19
20 // ボタン押下時の処理
21 func buttonPressed(sender: UIButton) {
22 print("Button Pressed!")
23 }
24
25 override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
26 for touch: AnyObject in touches {
27 // 座標取得
28 let point = touch.locationInView(self.view)
29 print(point)
30 }
31 }
32}
33
34
35// CustomButton Class
36class CustomButton: UIButton {
37
38 override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
39 super.touchesBegan(touches, withEvent: event)
40 self.superview!.touchesBegan(touches, withEvent: event)
41 }
42}
43
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/04/12 07:32