Animation and Focus issue

Hi,


I am having a bit of a problem wherein there is some unexpected, by me anyway, interaction between having a UIImageView's position animating and being able to focus on UIButtons. I have the following animation going on:


    func animateCloud1() {
        let duration : NSTimeInterval = 40.0
        let delay : NSTimeInterval = 0.0
        let options = UIViewAnimationOptions.CurveLinear
   
        let yPosition : CGFloat = CGFloat( arc4random_uniform(400))
   
        cloud1.frame = CGRectMake(-280, yPosition, 280, 320)
   
        UIView.animateWithDuration(duration, delay: delay, options: options, animations: {
            self.cloud1.frame = CGRectMake(2200, yPosition, 280, 320)
            }, completion: { animationFinished in
                self.animateCloud1()
        })
    }


The result of this code is a UIImageView (cloud1) moving from left to right across the screen. On each pass, it's y position is bein g changed randomly.


The problem is that while this animation is going on, I cannot move focus to one of the UIButtons on the screen ina predicatable manner. It seems that the UIImageView has focus and as a result no direction that I swipe in finds anything else to focus on.


Of even greater issue is the fact that whenever I am able to focus on one of the UIButtons, the UIImageView that is animating disappears as soon as the focus changes to one of the buttons.


In case it is relevant, I am changing the image of the UIButton receiving focus as follows:


    override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
        if (context.nextFocusedView == self) {
            //Handle losing focus
            if (self.tag == 0) {
                self.setImage(UIImage(named: "butUnfocused"), forState:UIControlState.Normal )
            }
        else {
            //Handle getting focus
            if (self.tag == 0) {
                self.setImage(UIImage(named: "butFocused"), forState:UIControlState.Normal )
            }


Any thoughts on what is going on would be greatly appreciated!

Replies

This is happening because when a UIView is animated what you see the presentation layer moving and not the model layer. The focus engine only considers the model later and it's frame when considering views for focus.