ã¯ããã«
ãååã¾ã§ã«iOSã¢ããªéçºã«ãããç»é¢ã®ä½ç½®é¢ä¿ã¨ã³ã³ãã³ãã®é ç½®ã«ã¤ãã¦èª¬æãã¾ãããé£è¼ç¬¬5åã§ã¯ãã¢ããªã®ç»é¢ã«UIé¨åãé ç½®ããæ¹æ³ï¼ãããã®æä½æã®å¦çã«ã¤ãã¦èª¬æãã¾ãã
ã対象èªè ãObjective-Cãåããæ¹ã¨ãã¦ããé¢ä¿ä¸ãObjective-Cèªä½ã®è¨èªä»æ§ãXcodeã®ä½¿ãæ¹ã«ã¤ãã¦ã¯è§£èª¬ãå²æããå ´åããããã¨ããäºæ¿ãã ãããåæ§ã«Swiftèªä½ã®èª¬æãå¿ ãããååã§ãªãå ´åãããã¾ãããã®ãããªå ´åã¯æ«å°¾ã®åèæç®çãåç §ãã¦ãã ããã
対象èªè
ãæ¬è¨äºã¯ã次ã®æ¹ã対象ã«ãã¦ãã¾ãã
- Objective-Cã®åºæ¬çãªããã°ã©ã ãåºæ¥ãæ¹
- Xcodeã使ããæ¹
UIé¨åã®å©ç¨
ãiOSã¢ããªã§ã¯ããã¿ã³ãå ¥åæ¬ã¨ãã£ãä½ããã®æä½ãè¡ãããã®UIã¯ãåé¨ååä½ã§ã¯ã©ã¹ãç¨æããã¦ãã¾ããã¯ã©ã¹å ã«ã¯ã該å½ããUIã®åä½æããã¼ã¿ã®ã»ããå¾ã®åããå¶å¾¡ããã¡ã½ãããç¨æããã¦ãããéçºè èªèº«ããã®ãããªæ©è½ãä½ãå¿ è¦ã¯ããã¾ããã
UIé¨åãé ç½®ãã
ãUIé¨åãUIViewã¯ã©ã¹ãç¶æ¿ãã¦ãã¾ãã®ã§ãåºæ¬çã«åæåå¦çãè¡ã£ãå¾ã«addSubviewã¡ã½ããã§ãªãã¸ã§ã¯ããç»é¢ã«é ç½®ãã¾ããç»é¢ã«ãã¿ã³ãé ç½®ããå¦çã¯æ¬¡ã®ããã«ãªãã¾ãã
// ä½ç½®ã¨ãµã¤ãºãæå®ãã¦ãã¿ã³ãåæå var button : UIButton = UIButton(frame:CGRectMake(100, 200, 80, 30)) // é常æã®ã¿ã¤ãã«ã¨è²ãæå® button.setTitle(" button ", forState: .Normal) button.setTitleColor(UIColor.grayColor(), forState: .Normal) // ãã¿ã³ãè§ä¸¸ã« button.layer.cornerRadius = 10 // ãã¼ãã¼ã®ç·ã®å¤ªããæå® button.layer.borderWidth = 1 // ç»é¢ã«é ç½® self.view.addSubview(button)
ãå®è¡çµæã¯æ¬¡ã®éãã§ãã
æä½æã®å¦çãæå®ãã
ãUIé¨åã®æä½æã®å¦çãæå®ããéã«ã¯ã主ã«æ¬¡ã®2ã¤ãå©ç¨ããã¾ãã
- 1ï¼addTarget(_:action:forControlEvents:)ã¡ã½ããã§ãUIé¨åã§çºçããã¤ãã³ãã«å¯¾ãã¦å¦çãè¡ã
- 2ï¼ããªã²ã¼ãã®ã¡ã½ããããªã¼ãã¼ã©ã¤ããã¦å¦çãè¡ã
ã1ï¼ã¯UIé¨åãç¶æ¿ãã¦ããUIControllerã¯ã©ã¹ã®ã¡ã½ãããå©ç¨ããå¦çã§ãã2ï¼ã®delegateã«é¢ãã¦ã¯ãUIé¨åãã¨ã«å©ç¨ã§ããdelegateã¯ã©ã¹ãç°ãªããå®è£ ã®ä»æ¹ãç°ãªãã¾ãã
ãããã§ã¯ã1ï¼ã«ã¤ãã¦èª¬æãã¾ãã2ï¼ã¯æ¬¡ç¯ã®ãµã³ãã«ã§èª¬æãã¾ãã
ã1ï¼ã®addTarget(_:action:forControlEvents:)ã¡ã½ããã¨ã¯ãUIé¨åã§çºçããã¤ãã³ãã«å¯¾ãã¦ä»»æã®å¦çãæå®ããã¡ã½ããã§ããã®æ¸å¼ã¯æ¬¡ã®ã¨ããã§ãã
UIé¨åãªãã¸ã§ã¯ã.addTarget(ã¢ã¯ã·ã§ã³ã®ã¿ã¼ã²ãã, action:ã»ã¬ã¯ã¿, forControlEvents:ã¤ãã³ã)
ãUIé¨åã§çºçããã¤ãã³ãã«ã¯æ¬¡ã®ãã®ãããã¾ãã
åå | æ¦è¦ |
---|---|
UIControlEventTouchDown | ã¿ãããã¦ã³ |
UIControlEventTouchDownRepeat | è¤æ°åã®ã¿ãããã¦ã³ |
UIControlEventTouchUpInside | ã³ã³ããã¼ã«å ã§ã®ã¿ããã¢ãã |
UIControlEventTouchUpOutside | ã³ã³ããã¼ã«å¤ã§ã®ã¿ããã¢ãã |
UIControlEventTouchDownRepeat | è¤æ°åã®ã¿ãããã¦ã³ |
UIControlEventTouchDragInside | UIä¸ã§ã®ãã©ãã° |
UIControlEventTouchDragOutside | UIã®å ããå¤ã¸ã®ãã©ãã° |
UIControlEventDragEnter | UIã®å¤ããå ã¸ã®ãã©ãã° |
UIControlEventValueChanged | å¤ãå¤åãã |
UIControlEventEditingDidBegin | ç·¨ééå§ |
UIControlEventEditingChanged | ç·¨éä¸ |
UIControlEventEditingDidEnd | ç·¨éçµäº |
UIControlEventEditingDidEndOnExit | ç·¨éçµäºå¾ãã©ã¼ã«ã¹ãªãæ |
UIControlEventAllEditingEvents | ç·¨ééå§æ |
UIControlEventAllTouchEvents | ã¿ãããã¹ã¦ |
UIControlEventAllEvents | ä¸è¨ãã¹ã¦ |
ãä¸è¨ã®ã¤ãã³ãã«å¯¾ãã¦å¦çãæå®ãããã¨ã§ãUIé¨åã®æä½ã«å¿ããå¦çãè¡ããã¨ãã§ãã¾ããå ·ä½çãªãµã³ãã«ã¯æ¬¡ã®éãã§ãã
// ãã¿ã³ãæ¼ããéã« onPush ã¡ã½ãããå®è¡ button.addTarget(self, action: "onPush:", forControlEvents:.TouchUpInside) ...ä¸ç¥... // ãã¿ã³ãæ¼ããéã®å¦ç func onPush(sender:UIButton) { // ã¢ãã¡ã¼ã·ã§ã³ãéå§ 1ç§ããã¦æåã¯ããããã«æå¾ã¯ä¸å®ã®åãã« UIView.animateWithDuration( 1.0, delay: 0.0, options: UIViewAnimationOptions.CurveEaseIn, animations: { // ãã¥ã¼ã®ä½ç½®ããµã¤ãºã®æ¡å¤§ãèæ¯è²ã®å¤æ´ãéæ度ã®å¤åãæå®ãã self.SampleView.frame = CGRectMake(200, 300, 50, 40); self.SampleView.transform = CGAffineTransformScale(self.SampleView.transform, 1.5, 1.5); self.SampleView.backgroundColor = UIColor.redColor() self.SampleView.alpha = 0.5; }, completion:{ // å¦çã®å®äºæã«ã¯ã¢ã©ã¼ããåºã (value: Bool) in var alert : UIAlertView = UIAlertView() alert.title = "info" alert.message = "ã¢ãã¡ã¼ã·ã§ã³çµäº" alert.addButtonWithTitle("OK") alert.show() } ); }
ãå®è¡çµæã¯æ¬¡ã®éãã§ãã
ãã»ã¬ã¯ã¿ã§æå®ãããã¡ã½ããã«ã¯ãå¼æ°ã¨ãã¦UIé¨åã®ãªãã¸ã§ã¯ãã渡ãã¾ããã»ã¬ã¯ã¿ã«ãã以å¤ã®æ å ±ã渡ãããå ´åã¯ãã°ãã¼ãã«å¤æ°ãã»ãã·ã§ã³çã®UIé¨å以å¤ã®ãªãã¸ã§ã¯ããå©ç¨ãã¾ãã