Advertisement
srk72

Radio Button Swift

Dec 21st, 2022
980
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.31 KB | None | 0 0
  1. @IBOutlet weak var radioButBoth: UIButton! {
  2.         didSet {
  3.             radioButBoth.setImage(UIImage(named:"unchecked"), for: .normal)
  4.             radioButBoth.setImage(UIImage(named:"checked"), for: .selected)
  5.         }
  6.     }
  7.  
  8.     @IBAction func radioButMethod(_ sender: UIButton) {
  9.         radioButBoth.isSelected = false
  10.         radioButOnline.isSelected = false
  11.         radioButOffline.isSelected = false
  12.         if sender == radioButOnline {
  13.             debugPrint("radioButOnline")
  14.             radioButOnline.checkboxAnimation {
  15.                
  16.             }
  17.         } else if sender == radioButOffline {
  18.             debugPrint("radioButOffline")
  19.             radioButOffline.checkboxAnimation {
  20.                
  21.             }
  22.         } else if sender == radioButBoth {
  23.             debugPrint("radioButBoth")
  24.             radioButBoth.checkboxAnimation {
  25.                
  26.             }
  27.         }
  28.     }
  29.  
  30. extension UIButton {
  31.     //MARK:- Animate check mark
  32.     func checkboxAnimation(closure: @escaping () -> Void){
  33.         guard let image = self.imageView else {return}
  34.        
  35.         UIView.animate(withDuration: 0.1, delay: 0.1, options: .curveLinear, animations: {
  36.             image.transform = CGAffineTransform(scaleX: 0.85, y: 0.85)
  37.            
  38.         }) { (success) in
  39.             UIView.animate(withDuration: 0.1, delay: 0, options: .curveLinear, animations: {
  40.                 self.isSelected = !self.isSelected
  41.                 //to-do
  42.                 closure()
  43.                 image.transform = .identity
  44.             }, completion: nil)
  45.         }
  46.     }
  47. }
  48.  
  49.     @discardableResult
  50.     private func checked(index: Int? = nil) -> Int {
  51.        
  52.         if index != nil {
  53.             if index == 1 {
  54.                 radioButOnline.isSelected = true
  55.             } else if index == 2 {
  56.                 radioButOffline.isSelected = true
  57.             } else {
  58.                 radioButBoth.isSelected = true
  59.             }
  60.         } else {
  61.             if radioButOnline.isSelected {
  62.                 return 1
  63.             } else if radioButOffline.isSelected {
  64.                 return 2
  65.             } else if radioButBoth.isSelected {
  66.                 return 3
  67.             }
  68.         }
  69.         return 0
  70.     }
  71.  
  72. let tuitionType:Int = checked()
  73.  
  74. self.checked(index: self.tutorDetail.tuition_type)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement