-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.

3
  • 1
    u r not setting frame properly...follow @Ashish.p's answer Commented Jul 24, 2015 at 7:40
  • You are creating a new instance of UIImageView in swift rather, you should be changing the frames only. Commented Jul 24, 2015 at 7:41
  • @iphonic agree: new instance of UIImageView with empty image data. Commented Jul 24, 2015 at 7:44

5 Answers 5

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 } } 
Sign up to request clarification or add additional context in comments.

3 Comments

Ok That great. Atleast my bigImageview is visible but still no animation in it.
ok. is there any reason for using dispatch_async in animation block?
Please look at the updated code. I have removed dispatch and everything works fine. So do we not use UI code on main thread in case of animation? If yes why not?
0

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

1 Comment

No it is not the issue of viewDidLoad or viewDidAppear. If so it should not work in objective c. Still tried and no success.
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.

Comments

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.

Comments

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 } } 

2 Comments

@TusharLimaye Try to animate in viewDidLayoutSubviews and viewDidAppear
@ParthAdroja Your answer not resolved. Still create new instance of UIImageView with no image data.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.