简体   繁体   中英

UIView animation with sub-subview

So I want to animate a subview of a container view in my main view (essentially a sub subview), should I create another view controller for the subview to control the sub subview or create an outlet in the main view controller for the sub subview? The problem is that I am triggering the animation with a UIButton, which also does some other things, so if I create another view controller, this UIButton would have two IBActions. Anyways, this is the code I have for the animation in the main viewController:

@IBAction func hourSelectorTap(gesture: UITapGestureRecognizer){
    if (isHourSelected){
        let path = UIBezierPath()
        path.moveToPoint(CGPoint(x:100, y:8))
        let anim = CAKeyframeAnimation(keyPath: "MoveZeBox")
        anim.path = path.CGPath
        self.MinHrView.layer.addAnimation(anim, forKey: "Move")
        isHourSelected = !isHourSelected

    } else{

    }

}

My first question to this would be, why have a container view if you're not placing some subclass of UIViewController in it? The main usage of embedding a view controller is so you can switch out which subclassed UIViewController is currently displaying. If you simply wanted nested views, I would suggest nesting UIViews and going from there.

Essentially the question is, you have some view controller that contains some subview, and you want to animate the subview, right? I would create an IBOutlet, say myView, for the view that I want to animate, and then in my function for the button press, do something like this:

@IBAction func hourSelectorTap(gesture: UITapGestureRecognizer){
 if (isHourSelected){
    UIView.animateWithDuration(0.2, animations: {
        //your animation code here
        self.myView.frame = CGRectMake(50,100,300,300)
    }, completion: {
        //anything you want to do when the animation is complete
    })
 } else {

 }
}

Otherwise, yes, the correct method for handling a view controller that is being embedded in your view is to subclass that view controller and place the code in there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM