-1

This seems to be a very weird issue. I have a small image view in storyBoard and have added tap gesture to it. On the action of gesture I am trying to add a different image view.

Objective C code-

-(void)tapImage{
    bigImageView = [[UIImageView alloc]init];
    [bigImageView setImage:[UIImage imageNamed:@"main.png"]];

    [bigImageView setFrame:CGRectMake(0, 0, 50, 50)];
    [self.view addSubview:bigImageView];


    [UIView animateWithDuration:2.0 delay:2.0 options:UIViewAnimationOptionCurveEaseInOut
                     animations:^(void) {

                         [bigImageView setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];                         //Leave it empty
                     }
                     completion:^(BOOL finished){

                         // Your code goes here

                     }];

}

Works totally fine.

Swift Code -

func imageTapped()
{
    println("Tapped on Image")

    bigImageView = UIImageView(frame: CGRectMake(0, 0, 50, 50))
    bigImageView.image = UIImage(named: "main")
    self.view.addSubview(self.bigImageView)


    UIView.animateWithDuration(2.0, delay: 2.0, options:UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
        dispatch_async(dispatch_get_main_queue()) {
            self.bigImageView = UIImageView(frame: CGRectMake(0, 0, 320, 500))
        }

    }) { (completed:Bool) -> Void in

    }
}

Does not work. I don't know where am I wrong. :(

UPDATE -

First I change the frame with "bigimageview.frame" so now it was displaying image view in gesture tap. But without animation.

So I removed dipatch on main thread and it was animating.

UIView.animateWithDuration(0.4, delay: 0.0, options:UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in

//            dispatch_async(dispatch_get_main_queue()) {
                self.bigImageView.frame = CGRectMake(0, 0, 320, 500)
//            }

            }) { (completed:Bool) -> Void in

        } }

But Still question remains why it happened. Are we not suppose to put UI changes on main thread inside closure/block? Some one please explain.

Tushar Limaye
  • 106
  • 2
  • 7

5 Answers5

0

Might be your delay and animation time is same so try this

func imageTapped() {
    print("Tapped on Image")

    bigImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
    bigImageView.image = UIImage(named: "main")
    self.view.addSubview(self.bigImageView)


    UIView.animate(withDuration: 2.0, delay: 1.0, options:UIView.AnimationOptions.curveEaseInOut, animations: { () -> Void in
        DispatchQueue.main.async {
            self.bigImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 320, height: 500))
        }
    }) { (completed:Bool) -> Void in

    }
}
Karen Hovhannisyan
  • 1,140
  • 2
  • 21
  • 31
Parth Adroja
  • 13,198
  • 5
  • 37
  • 71
0

viewDidLoad() is not the ideal place where you should animate views. viewDidAppear() method may works.

Leo
  • 24,596
  • 11
  • 71
  • 92
Pradumna Patil
  • 2,180
  • 3
  • 17
  • 46
  • No it is not the issue of viewDidLoad or viewDidAppear. If so it should not work in objective c. Still tried and no success. – Tushar Limaye Jul 24 '15 at 07:18
0

Replace this code

UIView.animateWithDuration(2.0, delay: 1.0, options:UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
        dispatch_async(dispatch_get_main_queue()) {
            self.bigImageView = UIImageView(frame: CGRectMake(0, 0, 320, 500))
        }

    }) { (completed:Bool) -> Void in

    } }

with

UIView.animateWithDuration(2.0, delay: 1.0, options:UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
        dispatch_async(dispatch_get_main_queue()) {

            bigImageView.frame = CGRectMake(0, 0, 320, 500)
        }

    }) { (completed:Bool) -> Void in

    } }
Ashish P.
  • 848
  • 5
  • 12
0

There is nothing to be gained by trying to use dispatch_async(). The way animations work you simply set the attributes in the specified animation block; the values are, in fact, set immediately and iOS uses the GPU where possible to animate the transition. If you run the block of code on a different thread it will be outside the scope of the animation and hence simply have an immediate effect.

Vorrade
  • 1
  • 1
0

AnimationWithDuration block takes all of its data(duration, options etc). Your dispatching block simply iterates separately. Thats why its not animating. Reason is simple: everything inside dispatch block iterates separately of other code because of stack system. Frame stopped, dispatch putted to stack, executed, and when it gets back to animation: hop, its already has final value and no animations needed.