HOME > UIControl
App開発には避けられないボタン等のコントロール群。それらを管理するUIControlクラスの説明をします。コントロールに プログラム上からイベントを送信させる処理も合わせて解説いたします。
NSObject
↑
UIResponder
↑
UIView
↑
UIControl
ーUIButton
ーUIDatePicker
ーUIPageControl
ーUIRefreshControl
ーUISegmentedControl
ーUISlider
ーUIStepper
ーUISwitch
ーUITextField
参考:UIKit Framework Reference UIControl Class Reference
参考:How To Make a Custom Control in Swift
参考:How to generate a button event in Swift?
参考:HOW TO BUILD A CUSTOM CONTROL IN IOS
参考:What is the difference between beginTrackingWithTouch and touchesBegan?
参考:Invoke target action in Swift
プロパティ名/型 | 説明 |
---|---|
contentVerticalAlignment |
アライメント(縦揃え)を設定する UIControlContentVerticalAlignment.Top:上寄せ(初期設定) UIControlContentVerticalAlignment.Center:中央寄せ UIControlContentVerticalAlignment.Bottom:下寄せ UIControlContentVerticalAlignment.Fill:縦いっぱい |
contentHorizontalAlignment |
アライメント(横揃え)を設定する UIControlContentHorizontalAlignment.Left:左寄せ(初期設定) UIControlContentHorizontalAlignment.Center:中央寄せ UIControlContentHorizontalAlignment.Right:左寄せ UIControlContentHorizontalAlignment.Fill:横いっぱい |
state |
選択されているかの取得。 UIControlState.Normal;通常(初期状態) UIControlState.Disabled;無効時 UIControlState.Selected:選択時 UIControlState.Highlighted:ハイライト時 ... |
selected |
選択状態かどうか。 stateのプロパティがUIControlState.Selectedかどうか |
enabled |
有効状態かどうか。 stateのプロパティがUIControlState.Disabledかどうか |
その他 |
Appleの公式ページを参照してください。 UIKit Framework Reference UIControl Class Reference |
メソッド名 | 説明 |
---|---|
addTarget | イベントをコントロールに登録する。例えばButtonコントロールにクリックイベントを追加する等。 例)button.addTarget(self, action: "btn_click:", forControlEvents:.TouchUpInside); |
removeTarget | イベントをコントロールから解除する。例えばButtonコントロールのタッチイベントを解放させる 例)button.removeTarget(self, action: "btn_click:", forControlEvents:.TouchUpInside); |
sendAction | コントロールをクリックしないでアクションを実行させる。Objective-CのperformSelectorと同じ 例)button.sendAction(Selector("btn_click:"), to: self, forEvent: nil); |
その他 |
Appleの公式ページを参照してください。 UIKit Framework Reference UIControl Class Reference |
sendAction(performSelector)はサンプルがあった方が分かりやすいのでコード共有。
var button = UIButton(frame: CGRectMake(0, 0, 100, 50));
button.backgroundColor = UIColor(red: 0.0, green: 1.0, blue: 0.0, alpha: 1.0);
button.addTarget(self, action: "btn_click:", forControlEvents:.TouchUpInside);
button.setTitle("Test", forState: UIControlState.Normal);
self.view.addSubview(button);
//以下一行実行でbtn_clickが送信されます
button.sendAction(Selector("btn_click:"), to: self, forEvent: nil);
//ボタンがクリックされると呼ばれます
func btn_click(sender: UIButton){
println("button is clcked!")
}
var button = UIButton(frame: CGRectMake(0, 0, 100, 50));
button.backgroundColor = UIColor(red: 0.0, green: 1.0, blue: 0.0, alpha: 1.0);
button.addTarget(self, action: "btn_click:", forControlEvents:.TouchUpInside);
button.setTitle("Test", forState: UIControlState.Normal);
self.view.addSubview(button);
//以下実行で同じことを処理できます。delayの数値を調整して待機後の実行も可能。以下は1.0秒待機してから実行
let delay = 1.0 * Double(NSEC_PER_SEC)
var time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
dispatch_after(time, dispatch_get_main_queue(), {
NSThread.detachNewThreadSelector(Selector("btn_click:"), toTarget:self, withObject: "button")
})
//ボタンがクリックされると呼ばれます
func btn_click(sender: UIButton){
println("button is clcked!")
}
NSThreadの方が使い勝手が良さそうな気もします。
2021-05-14 14:21:41 | The pitfalls of Filetransfer in watchconnectivity on WatchOS. And how to avoid.
AppleWatch it succeeds with the actual machine, but if it is a simulator, it will fail、、、
When I... |
2021-05-06 14:04:37 | Line's animation stamp production pitfalls 、、、 failure story
I tried to make a LINE sticker during Golden Week.
I made it and applied, but it was different f... |
2021-04-22 14:54:53 | Cool carousel carefully selected 3 points! Astringent image slider using WebGL.
I was on a trip to find a cool slider yesterday.
It's simple, easy to use, and has a good de... |
Tweet
![]() |
|||
|