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.
UIImageViewwith empty image data.