細かい仕様は分からないので、カスタムボタンを押下した時にタイマーを開始して、3秒後にカスタムボタンが乗っているViewController
に赤いView
を乗せるようにしました。
カスタムボタンの中で赤いView
を作成してsuperView
のViewController
に乗せることもできますが、
Viewの管理が複雑になってしまうので、ViewController
のメソッドを呼ぶようにしてViewController
で管理したほうが良いと思います。
swift
1 import UIKit
2
3 class ViewController : UIViewController {
4
5 let button1 = CustomButton ( type : . Custom )
6 var viewArray : [ UIView ] = [ ]
7
8 override func viewDidLoad ( ) {
9 super . viewDidLoad ( )
10
11 // ボタン1を生成
12 button1 . frame = CGRectMake ( 100 , 100 , 200 , 50 )
13 button1 . setTitle ( "Button1" , forState : . Normal )
14 button1 . setTitleColor ( UIColor . blueColor ( ) , forState : . Normal )
15 button1 . backgroundColor = UIColor . yellowColor ( )
16 button1 . addTarget ( self , action : # selector ( ViewController . buttonPressed ( _ : ) ) , forControlEvents : . TouchUpInside )
17 button1 . userInteractionEnabled = true
18 button1 . parent = self
19 button1 . tag = 1
20 view . addSubview ( button1 )
21 }
22
23 func buttonPressed ( sender : UIButton ) {
24 if ! button1 . isMoveing && sender . tag == 1 {
25 // ボタン1押下時のイベント
26 print ( "Push! Button1" )
27 }
28 }
29
30 func addView ( ) {
31 let redView = UIView ( frame : CGRectMake ( CGFloat ( 10 + ( viewArray . count * 55 ) ) , 100 , 50 , 50 ) )
32 redView . backgroundColor = UIColor . redColor ( )
33 view . addSubview ( redView )
34 viewArray . append ( redView )
35 }
36 }
37
38
39 // CustomButton Class
40 class CustomButton : UIButton {
41
42 var isMoveing : Bool = false
43 weak var parent : ViewController ?
44
45 var position : CGPoint !
46 var timer = NSTimer ( )
47
48 override func touchesBegan ( touches : Set < UITouch > , withEvent event : UIEvent ? ) {
49 super . touchesBegan ( touches , withEvent : event )
50 position = self . frame . origin
51 }
52
53 override func touchesMoved ( touches : Set < UITouch > , withEvent event : UIEvent ? ) {
54 super . touchesMoved ( touches , withEvent : event )
55
56 isMoveing = true
57
58 let touchEvent = touches . first !
59
60 // ドラッグ前の座標
61 let preDx = touchEvent . previousLocationInView ( superview ) . x
62 let preDy = touchEvent . previousLocationInView ( superview ) . y
63
64 // ドラッグ後の座標
65 let newDx = touchEvent . locationInView ( superview ) . x
66 let newDy = touchEvent . locationInView ( superview ) . y
67
68 // ドラッグしたx座標の移動距離
69 let dx = newDx - preDx
70
71 // ドラッグしたy座標の移動距離
72 let dy = newDy - preDy
73
74 // 画像のフレーム
75 var viewFrame : CGRect = self . frame
76
77 // 移動分を反映させる
78 viewFrame . origin . x + = dx
79 viewFrame . origin . y + = dy
80 self . frame = viewFrame
81 }
82
83 override func touchesEnded ( touches : Set < UITouch > , withEvent event : UIEvent ? ) {
84 super . touchesEnded ( touches , withEvent : event )
85 isMoveing = false
86 if position == self . frame . origin {
87 self . sendActionsForControlEvents ( . TouchUpInside )
88 timer = NSTimer . scheduledTimerWithTimeInterval ( 3 , target : self , selector : # selector ( CustomButton . timerAction ) , userInfo : nil , repeats : false )
89 }
90 }
91
92 func timerAction ( ) {
93 print ( "timerAction!" )
94 parent ? . addView ( )
95 }
96 }
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/05/04 08:16