I have an iPad app that is displaying a "note" (subclassed UILabel) with text on it. In order to navigate to the next note, I'd like it to slide it off the screen to the left while having the next one slide in from the right. I can get either animation to work, but not both at the same time.
Here's the code in my controller:
- (void)slideOutLeft {
// create the new note
flSlidingNote *newNote = [[flSlidingNote alloc] init];
newNote.text = #"blah blah";
CGRect newFrame = CGRectMake(1000, 70, 637, 297); // off the right
newNote.frame = newFrame;
[self.view addSubview:newNote];
// slide off the current one
CGRect currentFrameEnd = noteLabel.frame; // noteLabel is the existing note
currentFrameEnd.origin.x = 0 - noteLabel.frame.size.width; // off the left
[UIView animateWithDuration:1.0 animations:^{
noteLabel.frame = currentFrameEnd;
} completion:nil];
}
noteLabel does not animate at all. If I comment out the addSubview:newNote part it does. I'm still relatively new at this, so it's probably just something simple.
The problem happens whether newNote is animated or not (not animated in the code snippet).
You want to put the animation for both your views and the addSubview call for your new view in one animation block, like so:
// layout the new label like the old, but 300px offscreen right
UILabel * newNote = [[UILabel alloc] initWithFrame:CGRectOffset(self.noteLabelView.frame, 300, 0)];
newNote.text = #"NEW NOTE";
[UIView animateWithDuration:2.0f
delay:0
options:UIViewAnimationCurveEaseInOut
animations:^{
// animate the old label left 300px to offscreen left
self.noteLabelView.center = CGPointMake(self.noteLabelView.center.x-300, self.noteLabelView.center.y);
// add the new label to the view hierarchy
[self.view addSubview:newNote];
// animate the new label left 300px into the old one's spot
newNote.center = CGPointMake(newNote.center.x-300, newNote.center.y);
}
completion:^(BOOL finished) {
// remove the old label from the view hierarchy
[self.noteLabelView removeFromSuperview];
// set the property to point to the new label
self.noteLabelView = newNote;
}];
In this snippet above, I'm assuming the old label is addressable via the property self.noteLabelView.
(Also have a look at https://github.com/algal/SlidingNotes , though I can't promise that will stay there long so maybe SO isn't the right format for such a link? )
Adding to what Gabriele Petronella commented, here is a good resource on KeyFrames and AnimationGroup, with a sample GitHub project linked:
http://blog.corywiles.com/using-caanimationgroup-for-view-animations
This really helped me understand animations better.
not sure but i've seen other code that loops through subviews and performs animations, you may want to try the following:
for(UIView *view in self.subviews)
{
// perform animations
}
Related
I am trying to use UIViewController's transitionFromViewController:toViewController:duration method but with a custom animation.
I have the following two view controllers added as children to a custom container UIViewController:
firstController - This is an instance of UITabBarController
secondController - This is a subclass of UIViewController
The following code works as expected:
[self transitionFromViewController:firstController
toViewController:secondController
duration:2
options:UIViewAnimationOptionTransitionFlipFromLeft
animations:^(void){}
completion:^(BOOL finished){}];
However I would like to create a custom animation where the where firstController slides to the left and is replaced by secondController sliding in from the right similar to how UINavigationControllers push and pop methods work. After changing the options to UIViewAnimationOptionTransitionNone I have tried to implement custom animations in the animations block but have had absolutely no success. firstController is immediately swapped for secondController without and animations.
I would really appreciate any help.
Thank you
This is actually really easy. For some reason I assumed that the secondController's view would be be under/ behind that of firstController's. I had only tried to animate the firstController's view. This of course is wrong. As soon as transitionFromViewController:toViewController:duration is called secondController's view is placed over firstController's view. The following code works:
CGFloat width = self.view.frame.size.width;
CGFloat height = self.view.frame.size.height;
secondController.view.frame = CGRectMake(width, 0, width, height);
[self transitionFromViewController:firstController
toViewController:secondController
duration:0.4
options:UIViewAnimationOptionTransitionNone
animations:^(void) {
firstController.view.frame = CGRectMake(0 - width, 0, width, height);
secondController.view.frame = CGRectMake(0, 0, width, height);
}
completion:^(BOOL finished){
[secondController didMoveToParentViewController:self];
}
];
I have a view that is contained in the main view of my application (a square in the middle of a screen). I've written the following code that slides the view off the screen to the right, if you swipe it to the left:
- (IBAction)swipeLeft:(id)sender
{
CGRect initCardViewFrame = self.cardView.frame;
CGRect movedCardToRightViewFrame = CGRectMake(initCardViewFrame.origin.x + 1000, initCardViewFrame.origin.y, initCardViewFrame.size.width, initCardViewFrame.size.height);
[UIView animateWithDuration:0.7
animations:^{self.cardView.frame = movedCardToRightViewFrame;}
completion:nil];
}
This works great, however I would like to extend the code such that once the square goes off the right side of the screen, it comes right back from the left side back to the middle. I'm not really sure how to "re-draw" it on the left outside of the window, and then slide it back in the middle. I've tried just redoing the frames, but the animations only show the last frame movement in the animation block. I'm assuming that I need to push the view off the screen, then redraw it on the outside of the left hand of the screen, then slide it in the middle. Unless there is a more intuitive way to do it of course.
Update:
I've answered this question below, but I can't help but think there is a better way to do this without nesting UIView animations inside each other. Is this the only way to tackle this problem?
I ended up adding the following code inside the "completion" parameter:
completion:^(BOOL finished)
{
self.cardView.frame = moveCardToLeftViewFrame;
[UIView animateWithDuration:0.4
animations:^{self.cardView.frame = initCardViewFrame;}
completion:nil];
}];
Note* moveCardToLeftViewFrame is a CGRect that places the view to the left of the visible window. So after it slides to the right, it is placed outside of screen to the left, then slides back into the middle.
you can use this code in case :
view will go out off screen to Left and Get In From Right.
view will go out off screen to Right and Get In From Left.
code sniped:
- (void)animateViewWithTransformation:(MyEnumAnimationTransformation)animationTransformation withDuration:(CGFloat)duration {
CGFloat goOutOffScreenToX, cameInToScreenFromX;
switch (animationTransformation) {
case goOutToLeftGetInFromRight:
goOutOffScreenToX = -1 * [UIScreen mainScreen].applicationFrame.size.width;
cameInToScreenFromX = [UIScreen mainScreen].applicationFrame.size.width;
break;
case goOutToRightGetInFromLeft:
goOutOffScreenToX = [UIScreen mainScreen].applicationFrame.size.width;
cameInToScreenFromX = -1 * [UIScreen mainScreen].applicationFrame.size.width;
break;
default:
break;
}
CGRect originViewFrame = self.frame;
[UIView animateWithDuration:duration
animations:^{[self setX:goOutOffScreenToX];}
completion:^(BOOL finished)
{
[self setX:cameInToScreenFromX];
[UIView animateWithDuration:duration
animations:^{[self setX:originViewFrame.origin.x];}
completion:nil];
}];
}
The following code has a (at least one) deep flaw: Once CGFrame is set it's location is fixed.
-(UIImageView*) ownImageView {
if (ownImageView == nil) {
ownImageView = [[UIImageView alloc] initWithFrame:
CGRectMake(self.xPosition, self.yPosition,
[constants cardWidth], [constants cardHeight])];
[ownImageView setImage:self.faceImage];
}
return ownImageView;
}
I would like to reuse the UIImageView (once created, I don't want to realloc it). Instead, i'd like to change x,y coordinates of the frame it contains.
The following code works fine, however, it seems carelessly wasteful
-(UIImageView*) ownImageView {
[ownImageView removeFromSuperview];
ownImageView = [[UIImageView alloc] initWithFrame:
CGRectMake(self.xPosition, self.yPosition,
[constants cardWidth], [constants cardHeight])];
[ownImageView setImage:self.faceImage];
return ownImageView;
}
Question: How would you handle this, while keeping as much of lazy instantiation as possible?
The view represents a 2d rectangle moving along the screen. I'd like to represent the "move" by simply changing X/Y position of the existing view instead of replacing it with a new copy.
ownImageView.frame = CGRectMake(x,y,w,h);
If you want to move it the simplest thing to do is to let Core Animation do the work for you.
For more details and options check UIView Class Reference
[UIView animateWithDuration:2
delay:0
options:UIViewAnimationCurveEaseInOut
animations:^{
self.THE_VIEW.center = CGPointMake(200, 200);
// OR you can change it's frame if you prefer
}
completion:NULL];
I have a main view that is supposed to display several subviews. Those subviews are directly above and below one another (in the z axis) and will drop down (in the y axis) and move up using this code:
if (!CGRectIsNull(rectIntersection)) {
CGRect newFrame = CGRectOffset (rectIntersection, 0, -2);
[backgroundView setFrame:newFrame];
} else{
[viewsUpdater invalidate];
viewsUpdater = nil;
}
rectIntersection is used to tell when the view has entirely moved down and is no longer behind the front one (when they no longer overlap rectIntersection is null), it moves down by 2 pixels at a time because this is all inside a repeating timer. I want my main view, the one that contains these two other views, to resize downward so that it expands just as the view in the background is being lowered. This is the code I'm trying for that:
CGRect mainViewFrame = [mainView frame];
if (!CGRectContainsRect(mainViewFrame, backgroundFrame)) {
CGRect newMainViewFrame = CGRectMake(0,
0,
mainViewFrame.size.width,
(mainViewFrame.size.height + 2));
[mainView setFrame:newMainViewFrame];
}
The idea is to check if the mainView contains this background view. When the backgroundView is lowered, the main view no longer contains it, and it (should) expand downward by 2 pixels. This would happen until the background view stopped moving and the mainView finally contains backgroundView.
The problem is that the mainView is not resizing at all. the background view is being lowered, and I can see it until it disappears off the bottom of the mainView. mainView should have resized but it does not change in any direction. I tried using setFrame and setBounds (with and without setNeedsDisplay) but nothing worked.
I'm really just looking for a way to programmatically change the size of the main view.
I think I understood, what the problem is. I read carefuly the code.
if (!CGRectIsNull(rectIntersection)) {
// here you set the wrong frame
//CGRect newFrame = CGRectOffset (rectIntersection, 0, -2);
CGRect newFrame = CGRectOffset (backgroundView.frame, 0, -2);
[backgroundView setFrame:newFrame];
} else{
[viewsUpdater invalidate];
viewsUpdater = nil;
}
rectIntersection is actually the intersection of the two views, that overlap, and as the backgroundView is moved downward, that rect's height decreases.
That way the mainView gets resized only one time.
To add on this, here is a simple solution using block syntax, to animate your views, this code would typically take place in your custom view controller.
// eventually a control action method, pass nil for direct call
-(void)performBackgroundViewAnimation:(id)sender {
// first, double the mainView's frame height
CGFrame newFrame = CGRectMake(mainView.frame.origin.x,
mainView.frame.origin.y,
mainView.frame.size.width,
mainView.frame.size.height*2);
// then get the backgroundView's destination rect
CGFrame newBVFrame = CGRectOffset(backgroundView.frame,
0,
-(backgroundView.frame.size.height));
// run the animation
[UIView animateWithDuration:1.0
animations:^{
mainView.frame = newFrame;
backgroundView.frame = newBVFrame;
}
];
}
I have two UIImageViews located about center of a horizontal screen and when the user clicks a button another UIImageView, located off screen, slides in from the right. I'm am just trying to detect when the view being brought onto the screen collides with the two static views. The problem is when I run my code and check the CGRect frames they are returned from where the views start, not end, regardless of where I place the frame calls in my method or even if I place them outside the method in a separate one. I'm a bit new to Obj-C and I understand that Core Animation runs on a separate thread and I am guessing that is why I am getting the starting values. (Correct me if I am wrong here).
So, I guess the question here is how do I detect collisions when one item is static and another is animated. Here's my code (feel free to clean it up):
- (IBAction)movegirl
{
//disabling button
self.movegirlbtn.enabled = NO;
//getting graphics center points
CGPoint pos = bushidogirl.center;
CGPoint box1 = topbox.center;
CGPoint box2 = bottombox.center;
//firing up animation
[UIView beginAnimations: nil context: NULL];
//setting animation speed
[UIView setAnimationDuration:2.0 ];
//setting final position of bushidogirl, then resetting her center position
pos.x = 260;
bushidogirl.center = pos;
//running animations
[UIView commitAnimations];
//playing gong sound
[self.audioplayer play];
//enabling button
self.movegirlbtn.enabled = YES;
[self collisiondetection: bushidogirl : topbox];
}
- (void)collisiondetection:(UIImageView *)item1 : (UIImageView *)item2
{
CGRect item1frame = item1.frame;
CGRect item2frame = item2.frame;
NSLog(#"Item1 frame = %d and Item 2 frame = %d", item1frame, item2frame);
}
How about:
if (CGRectIntersectsRect(item1frame, item2frame))
{
// collision!!
}