How to use UIControl class

Best Mockingbird code snippet using .UIControl

Run Mockingbird automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

UIControl+Actions.swift

Source: UIControl+Actions.swift Github

copy
1//
2//  UIControl+Actions.swift
3//  AirPay
4//
5//  Created by KD Chen on 28/8/17.
6//  Copyright © 2017 Quest Payment Systems Pty Ltd. All rights reserved.
7//
8
9import UIKit
10import ObjectiveC
11
12typealias PTControlBlock = () -> Void
13
14extension UIControl {
15
16    // The extension provides shortcut functions for addTarget() method to all UIControl based components
17 
18    // MARK: Public
19    @discardableResult func bind(_ block: @escaping PTControlBlock, for event: UIControlEvents) -> UIControl {
20        self.add(block, for: event)
21        return self
22    }
23
24    @discardableResult func unBind(for event: UIControlEvents) -> UIControl {
25        self.remove(for: event)
26        return self
27    }
28
29    // MARK: Private
30    fileprivate func add(_ block: @escaping PTControlBlock, for event: UIControlEvents) {
31        guard let action = self.action(for: event) else {
32            return
33        }
34        var s = self.EventsBlocks
35        s[event.rawValue] = block
36        self.EventsBlocks = s
37        self.addTarget(self, action: action, for: event)
38    }
39
40    fileprivate func remove(for event: UIControlEvents) {
41        guard let action = self.action(for: event) else {
42            return
43        }
44        var s = self.EventsBlocks
45        s.removeValue(forKey: event.rawValue)
46        self.EventsBlocks = s
47        self.removeTarget(self, action: action, for: event)
48    }
49
50    fileprivate static var eventsBlocksKey = "eventsBlocksKey"
51    fileprivate var EventsBlocks: [AnyHashable:Any] {
52        get {
53            guard let s = objc_getAssociatedObject(self, &UIControl.eventsBlocksKey) else {
54                let result = [AnyHashable: Any]()
55                objc_setAssociatedObject(self, &UIControl.eventsBlocksKey, result, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
56                return result
57            }
58            return s as! [AnyHashable : Any]
59        }
60        set {
61            objc_setAssociatedObject(self, &UIControl.eventsBlocksKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
62        }
63    }
64
65    fileprivate func action(for event: UIControlEvents) -> Selector? {
66        switch event {
67        case UIControlEvents.touchDown: return #selector(touchDown)
68        case UIControlEvents.touchDownRepeat: return #selector(touchDownRepeat)
69        case UIControlEvents.touchUpInside: return #selector(touchUpInside)
70        case UIControlEvents.touchUpOutside: return #selector(touchUpOutside)
71        case UIControlEvents.touchCancel: return #selector(touchCancel)
72        case UIControlEvents.valueChanged: return #selector(valueChanged)
73        case UIControlEvents.editingDidBegin: return #selector(editingDidBegin)
74        case UIControlEvents.editingChanged: return #selector(editingChanged)
75        case UIControlEvents.editingDidEnd: return #selector(editingDidEnd)
76        case UIControlEvents.editingDidEndOnExit: return #selector(editingDidEndOnExit)
77        default: return .none
78        }
79    }
80
81    @objc fileprivate func touchDown() {
82        if let block = EventsBlocks[UIControlEvents.touchDown.rawValue] as? PTControlBlock {
83            block()
84        }
85    }
86
87    @objc fileprivate func touchDownRepeat() {
88        if let block = EventsBlocks[UIControlEvents.touchDownRepeat.rawValue] as? PTControlBlock {
89            block()
90        }
91    }
92
93    @objc fileprivate func touchUpInside() {
94        if let block = EventsBlocks[UIControlEvents.touchUpInside.rawValue] as? PTControlBlock {
95            block()
96        }
97    }
98
99    @objc fileprivate func touchUpOutside() {
100        if let block = EventsBlocks[UIControlEvents.touchUpOutside.rawValue] as? PTControlBlock {
101            block()
102        }
103    }
104
105    @objc fileprivate func touchCancel() {
106        if let block = EventsBlocks[UIControlEvents.touchCancel.rawValue] as? PTControlBlock {
107            block()
108        }
109    }
110
111    @objc fileprivate func valueChanged() {
112        if let block = EventsBlocks[UIControlEvents.valueChanged.rawValue] as? PTControlBlock {
113            block()
114        }
115    }
116
117    @objc fileprivate func editingDidBegin() {
118        if let block = EventsBlocks[UIControlEvents.editingDidBegin.rawValue] as? PTControlBlock {
119            block()
120        }
121    }
122
123    @objc fileprivate func editingChanged() {
124        if let block = EventsBlocks[UIControlEvents.editingChanged.rawValue] as? PTControlBlock {
125            block()
126        }
127    }
128
129    @objc fileprivate func editingDidEnd() {
130        if let block = EventsBlocks[UIControlEvents.editingDidEnd.rawValue] as? PTControlBlock {
131            block()
132        }
133    }
134
135    @objc fileprivate func editingDidEndOnExit() {
136        if let block = EventsBlocks[UIControlEvents.editingDidEndOnExit.rawValue] as? PTControlBlock {
137            block()
138        }
139    }
140
141}
142
Full Screen

RPDraggableButton.swift

Source: RPDraggableButton.swift Github

copy
1//
2//  RPDraggableButton.swift
3//  RPViews
4//
5//  Created by Romana on 21/1/21.
6//
7
8import Foundation
9import UIKit
10
11public enum CustomDraggableButtonType {
12    case DraggableRect
13    case DraggableRound
14}
15
16public  protocol CustomDraggableButtonProtocol {
17    func onClick()
18}
19
20@IBDesignable public class CustomDraggableButton: UIButton {
21    
22    var customDraggableButtonProtocol:CustomDraggableButtonProtocol?
23    
24    @IBInspectable public var rectType: Bool = true { didSet{ updateUI() } }
25    
26    var btnHalfHt: CGFloat = 0
27    var btnHalfWd: CGFloat = 0
28    let minX: CGFloat = 1
29    let minY: CGFloat = 1
30    var maxX: CGFloat = 0
31    var maxY: CGFloat = 0
32    
33    
34    override init(frame: CGRect) {
35        super.init(frame: frame)
36        self.initCustomView()
37    }
38    
39    required init?(coder aDecoder: NSCoder) {
40        super.init(coder: aDecoder)
41        self.initCustomView()
42    }
43    
44    fileprivate func updateUI() {
45        
46        btnHalfHt = (frame.height/2)
47        btnHalfWd = (frame.width/2)
48        
49        if rectType {
50            self.layer.cornerRadius = 18.0
51        }else{
52            self.layer.cornerRadius = (frame.height/2)
53            self.clipsToBounds = true
54        }
55        
56        setNeedsDisplay()
57    }
58    
59    fileprivate func initCustomView() {
60        
61        let color = UIColor.white
62        self.setTitleColor(color, for: .normal)
63        self.setTitle(self.titleLabel?.text?.capitalized, for: .normal)
64        
65        self.layer.backgroundColor = UIColor.gray.cgColor
66        
67        //        addTarget(self, action: #selector(touchDragEnter(control:event:)), for: UIControlEvents.touchDragEnter)
68        //        addTarget(self, action: #selector(touchDragInside(control:event:)), for: UIControlEvents.touchDragInside)
69        //        addTarget(self, action: #selector(touchDragOutside(control:event:)), for: UIControlEvents.touchDragOutside)
70        //        addTarget(self, action: #selector(touchDragExit(control:event:)), for: UIControlEvents.touchDragExit)
71        //        addTarget(self, action: #selector(touchUpOutside(control:event:)), for: UIControlEvents.touchUpOutside)
72        //        addTarget(self, action: #selector(touchDown(control:event:)), for: UIControlEvents.touchDown)
73        //        addTarget(self, action: #selector(touchCancel(control:event:)), for: UIControlEvents.touchCancel)
74        //        addTarget(self, action: #selector(touchDownRepeat(control:event:)), for: UIControlEvents.touchDownRepeat)
75        //        addTarget(self, action: #selector(editingDidBegin(control:event:)), for: UIControlEvents.editingDidBegin)
76        //        addTarget(self, action: #selector(editingChanged(control:event:)), for: UIControlEvents.editingChanged)
77        //        addTarget(self, action: #selector(editingDidEnd(control:event:)), for: UIControlEvents.editingDidEnd)
78        //        addTarget(self, action: #selector(editingDidEndOnExit(control:event:)), for: UIControlEvents.editingDidEndOnExit)
79        //        addTarget(self, action: #selector(valueChanged(control:event:)), for: UIControlEvents.valueChanged)
80        
81        addTarget(self, action: #selector(drag(control:event:)),
82                  for: [UIControl.Event.touchDragInside,
83                        UIControl.Event.touchDragExit,
84                        UIControl.Event.touchDragOutside])
85        
86        addTarget(self, action: #selector(touchUpInside(control:event:)), for: UIControl.Event.touchUpInside)
87        
88    }
89    
90    var isDraged: Bool = false
91    
92    
93    
94    @objc func drag(control: UIControl, event: UIEvent) {
95        
96        isDraged = true
97        
98        if let center = event.allTouches?.first?.location(in: self.superview) {
99            control.center = center
100        }
101        
102        maxX = (self.superview?.frame.width)! - 1
103        maxY = (self.superview?.frame.height)! - 1
104        
105        if ((self.frame.origin.x + btnHalfWd) > maxX) {
106            control.center.x = (maxX - btnHalfWd)
107        }
108        if ((self.frame.origin.x - btnHalfWd) < minX) {
109            control.center.x = (minX + btnHalfWd);
110        }
111        if ((self.frame.origin.y + btnHalfHt) > maxY) {
112            control.center.y = (maxY - btnHalfHt)
113        }
114        if (self.frame.origin.y - btnHalfHt < minY) {
115            control.center.y = (minY + btnHalfHt);
116        }
117    }
118    
119    @objc func touchUpInside(control: UIControl, event: UIEvent) {
120        
121        if isDraged {
122            isDraged = false
123        }else{
124            customDraggableButtonProtocol?.onClick()
125        }
126    }
127    
128    //    func touchDragEnter(control: UIControl, event: UIEvent) { print("touchDragEnter") }
129    //    func touchDragOutside(control: UIControl, event: UIEvent) { print("touchDragOutside") }
130    //    func touchDragExit(control: UIControl, event: UIEvent) {  print("touchDragExit") }
131    //    func touchUpOutside(control: UIControl, event: UIEvent) { print("touchUpOutside") }
132    //    func touchDown(control: UIControl, event: UIEvent) { print("touchDown") }
133    //    func touchCancel(control: UIControl, event: UIEvent) { print("touchCancel") }
134    //    func touchDownRepeat(control: UIControl, event: UIEvent) { print("touchDownRepeat") }
135    //    func editingDidBegin(control: UIControl, event: UIEvent) { print("editingDidBegin") }
136    //    func editingChanged(control: UIControl, event: UIEvent) { print("editingChanged") }
137    //    func editingDidEnd(control: UIControl, event: UIEvent) { print("editingDidEnd") }
138    //    func editingDidEndOnExit(control: UIControl, event: UIEvent) { print("editingDidEndOnExit") }
139    //    func valueChanged(control: UIControl, event: UIEvent) { print("valueChanged") }
140    
141}
142
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Most used methods in UIControl

    No methods found for this class 😞

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)