I've 2 views,A and B.
A(Aframe)--------------->B(Bframe)------>C(cFrame)
I want to execute an animation.A will moves from Aframe to Cframe.
But at B,A will be hidden
A(Aframe)------(non-hidden area)---------->B(Bframe)------(hidden area)-------->C(Cframe)
How to do that?
Thanks so much.
// I suppose viewA, viewB, viewC is subviews of self.view
UIView *viewA = ..., *viewB = ..., *viewC = ...;
[self.view bringSubviewToFront:viewA];
[UIView animateWithDuration:0.5 animations:^{
viewA.frame = viewC.frame;
} completion:^(BOOL finished) {
[self.view sendSubviewToBack:viewA];
}];
Related
I have a custom view (which draws callouts/bubbles) that I currently add as a subview to a UIImageView. It works as intended but I would like to animate the operation with a spring-like effect. I am using the below code but it does not work (the block gets executed but without animation):
/* ViewController */
UIImageView *iv = self.imageView;
ZGCBubbleView *bubbleView = [[ZGCBubbleView alloc] init];
bubbleView.hidden = YES;
[iv addSubview:bubbleView];
// UIView animation test
[UIView animateWithDuration:2.0
delay:0
usingSpringWithDamping:0.5
initialSpringVelocity:0.5
options:UIViewAnimationOptionAllowAnimatedContent & UIViewAnimationOptionLayoutSubviews
animations:^{
bubbleView.hidden = NO;
[self.view layoutIfNeeded]; // tried this
}
completion:nil];
I have tried to animate both the hidden property as well as the alpha properties but same result. I have also tried animating the addSubview method, no difference. I started out with a more simple animation as proof of concept but that didn't work either.
The above code is executed within a method which is called during the (void)viewDidAppear:(BOOL)animated cycle. Does this have anything to do with this in that the animation is being executed during the main thread? I have read something to that effect but unsure. Also, I am using auto layout for the UIImageView but didn't think that would matter in this case since the animation is being applied to a custom subview of the UIImageView.
Any help appreciated.
Better use alpha.
UIImageView *iv = self.imageView;
ZGCBubbleView *bubbleView = [[ZGCBubbleView alloc] init];
bubbleView.alpha = 0;
// bubbleView.hidden = YES;
[iv addSubview:bubbleView];
// UIView animation test
[UIView animateWithDuration:2.0
delay:0
usingSpringWithDamping:0.5
initialSpringVelocity:0.5
options:UIViewAnimationOptionAllowAnimatedContent & UIViewAnimationOptionLayoutSubviews
animations:^{
// bubbleView.hidden = NO;
bubbleView.alpha = 1;
//[self.view layoutIfNeeded]; // tried this
}
completion:nil];
Try with this.
UIButton *catchButton = (UIButton *)sender;
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut//UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat |
animations:^{
catchButton.alpha = 0.4;
catchButton.enabled = NO;
}
completion:NULL];
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
UIImageView *circleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"face.png"]];
circleView.frame = CGRectMake(0, 0, 200, 200);
circleView.layer.cornerRadius = 100;
circleView.center = self.view.center;
[self.view addSubview:circleView];
circleView.transform = CGAffineTransformMakeScale(0, 0);
[UIView animateWithDuration:1 delay:0 usingSpringWithDamping:.5 initialSpringVelocity:1 options:0 animations:^{
circleView.transform = CGAffineTransformMakeScale(1, 1);
} completion:nil];
As you can see, I made a view called circleView in this animation method. Then, I created a touchesBegan method and I want to run through the animation again when touching the screen. How should I do that?
As Sujay said, create a new method and put below lines inside that method. Call that method from touchBegan method.
circleView.transform = CGAffineTransformMakeScale(0, 0);
[UIView animateWithDuration:1 delay:0 usingSpringWithDamping:.5 initialSpringVelocity:1 options:0 animations:^{
circleView.transform = CGAffineTransformMakeScale(1, 1);
} completion:nil];
I did it. I created 2 properties and I made an animation where it gets small, then another animation that makes to make it bigger.
With the following code I'm showing a view with an animation on a button click.
UIViewController *modalView = self.pageViewController;
[self.view addSubview:modalView.view];
[UIView animateWithDuration:1 animations:^{
CGRect currentRect = modalView.view.frame;
currentRect.origin.y = 650.0f;
currentRect.size.height = 295.0f;
[modalView.view setFrame:currentRect];
[modalView.view removeFromSuperview];
}];
[self.view addSubview:_pageViewController.view];
[self.pageViewController didMoveToParentViewController:self];
This works fine for first time button click. When I'm pressing the button again and again this is not animating anymore. It's animating for the first time only. Is there any way to do so?
Thanks in Advance!
The cause is that modalView.view's frame initially if different from
currentRect.origin.y = 650.0f;
currentRect.size.height = 295.0f;
so when changes its frame animation is happens.
But next time when you perform button's action modalView.view's current frame and that frame you sets are same. So there is nothing to animate at all.
Also, you should move [modalView.view removeFromSuperview]; from animation block to completion block
[UIView animateWithDuration:1 animations:^{
CGRect currentRect = modalView.view.frame;
currentRect.origin.y = 650.0f;
currentRect.size.height = 295.0f;
[modalView.view setFrame:currentRect];}
completion:^(BOOL finished) {
[modalView.view removeFromSuperview];
}];
I'm using the following code to zoom/enlarge an image from within a table view. I would like it to enlarge out to full screen. the current problem is that when it enlarges it enlarges only within the table cell (and as a result is partially hidden beneath the next table cell) instead of the full view.
-(void)zoomPhotoMethod :(id) sender
{
UITapGestureRecognizer *gesture = (UITapGestureRecognizer *) sender;
NSLog(#"Tag = %d", gesture.view.tag);
userSubmittedImageView = (UIImageView *)gesture.view;
if (!isFullScreen) {
[UIView animateWithDuration:0.5 delay:0 options:0 animations:^{
//save previous frame
prevFrame = userSubmittedImageView.frame;
UIView *popupImageViewForTableCell = [[UIView alloc] initWithFrame: CGRectMake ( 0, 0, 320, 500)];
[userSubmittedImageView setFrame:[popupImageViewForTableCell bounds]];
}completion:^(BOOL finished){
isFullScreen = TRUE;
}];
return;
}
else{
[UIView animateWithDuration:0.5 delay:0 options:0 animations:^{
[userSubmittedImageView setFrame:prevFrame];
}completion:^(BOOL finished){
isFullScreen = FALSE;;
}];
return;
}
}
updated code:
-(void)zoomPhotoMethod :(id) sender
{
UITapGestureRecognizer *gesture = (UITapGestureRecognizer *) sender;
NSLog(#"Tag = %d", gesture.view.tag);
userSubmittedImageView = (UIImageView *)gesture.view;
if (!isFullScreen) {
[UIView animateWithDuration:0.3 delay:0 options:0 animations:^{
//save previous frame
prevFrame = userSubmittedImageView.frame;
newView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)];
[self.view addSubview:newView];
UIView *popupImageViewForTableCell = [[UIView alloc] initWithFrame: CGRectMake ( 0, 0, 320, 500)];
[userSubmittedImageView setFrame:[popupImageViewForTableCell bounds]];
[newView addSubview:userSubmittedImageView];
[userSubmittedImageView setFrame:[newView bounds]];
}completion:^(BOOL finished){
isFullScreen = TRUE;
}];
return;
}
else{
[UIView animateWithDuration:0.3 delay:0 options:0 animations:^{
[userSubmittedImageView setFrame:prevFrame];
[newView setFrame:prevFrame];
}completion:^(BOOL finished){
isFullScreen = FALSE;;
}];
return;
}
}
What's happening is that you are still a subview of the tableview cell, so it's not going to be able to go outside of those bounds without being clipped.
To do what you want to do, you're going to have to:
Create a new UIImageView outside of your table view (add it as a subview outside of the tableview).
Set the image in the image view to the same image that you have in the tableview cell.
Start it at the same size and position as your tableview cell image.
Animate it up to where you want it.
When it's time to remove it, move it back down to the original tableview cell image.
Discard the imageview.
One way would be to use a copy of the image and place it outside the cell just above your original and animate this second image to full screen.
Another way would be to increase the tableView rowHeight and reload the section.
I'm trying to create sort of a slide-out slide-in motion for my views when the user presses the forward or backwards button.
I made the first view slide out just fine, but the second one just won't slide in.
Here's my code:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDelay:0.1];
[UIView setAnimationDuration:0.7];
UIView *currentView = [[contentScrollView subviews] objectAtIndex:0];
currentView.center = CGPointMake(contentScrollView.frame.size.width+contentScrollView.frame.size.width/2, currentView.center.y);
UIView *newView = [[contentScrollView subviews] objectAtIndex:1];
newView.center = CGPointMake(newView.center.x+newView.frame.size.width, newView.center.y);
[UIView commitAnimations];
I don't really see what the problem is, it should work. But the currentView just slides out, and the newView is still out in the -x values, hidden from the user.
I already tried changing to transformations, but that didn't do anything.
What could cause this?
Try this code (tested):
Copy and paste it to see it in action and adjust the frames for your views to suit your needs.
UIView *viewOne = [[UIView alloc] initWithFrame:(CGRect){{0, 0}, 200, 200}]; //currentView
UIView *viewTwo = [[UIView alloc] initWithFrame:(CGRect){{200, 0}, 200, 200}]; //newView
viewOne.backgroundColor = [UIColor lightGrayColor];
viewTwo.backgroundColor = [UIColor darkGrayColor];
[self addSubview:viewOne];
[self addSubview:viewTwo];
[UIView animateWithDuration:5.0f delay:0.0f options:UIViewAnimationOptionCurveEaseInOut animations:^
{
viewOne.frame = (CGRect){{300, 0}, 200, 200};
viewTwo.frame = (CGRect){{-100, 0}, 200, 200};
} completion:^(BOOL finished) {
}];
This is to show you how these two view sliding in and out.
You have to adjust the frames for your views accordingly to your needs.