Is it possible to animate the bounds of the CALayer on iPhone? How to implement that? Thanks!
Yes, it is possible.
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0f];
[[self view] setBounds:CGRectMake(0.0f, 0.0f, 200.0f, 200.0f)];
[UIView commitAnimations];
This will animate a view controller's view from it's current bounds to a bounds of 200 x 200 over 1 second. It won't change the origin--just the size of the bounds rectangle. This is implicit animation by the way. If you want a more complicated animation, look at using CABasicAnimation and animating explicitly.
Are you trying to animate the visible part of an image inside a layer, were the layer itself maintains size and position?
That's what CAScrollLayer is made for. Use a CAScrollLayer in place of your current Layer and add image-rendering layerl as a subLayer to the CAScrollLayer. You can then use the transform property of the sublayer to achieve that effect.
Related
I have seen quite a few tutorials on how to fade images in and out on the iPhone simply by changing their alpha values (for example here).
Thats ok, however I am trying to achieve something slightly different; instead of the whole image fading in/out, I would the image to be animated so that over time it is displayed from the centre of the image to the edge (and vice versa).
So at the start of the animation I would like the image to not be displayed on the screen and then over the duration of the animation starting from the centre bit by bit the image is displayed until we reach the end and the whole image is displayed on screen.
I cant find anything along those lines, does anyone know if that would be possible on the iPhone? Any ideas/links that might be useful?
It should be easy enough to achieve — create a UIImageView holding the thing, set contentMode to UIViewContentModeCenter, then use a CoreAnimation block to animate a change in the frame from being of zero width/height and centred to being the full area you want to cover.
E.g.
/* coming into here, imageView is a suitable UIImageView,
frameToFill is a CGRect describing the area the image
view should cover when fully unfurled */
// set up image view: content mode is centre so that the current
// frame doesn't affect image sizing, and we'll be keeping the same
// centre throughout so it won't move. The initial frame is on the
// centre and of zero size. The view contents are clipped to the view's
// bounds so that the changing frame does actually cut off the image
imageView.contentMode = UIViewContentModeCenter;
imageView.frame = CGRectMake(frameToFill.origin.x + frameToFill.size.width*0.5f,
frameToFill.origin.y + frameToFill.size.height*0.5f,
0.0f, 0.0f);
imageView.clipsToBounds = YES;
// set up an animation block, lasting 3 seconds, in which the
// only thing animated is the view's frame, expanding back out
// to the original
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:3.0f];
imageView.frame = frameToFill;
[UIView commitAnimations];
The edge of your view will be hard and rectangular. If you want anything more subtle, you're probably not going to be able to achieve it with CoreAnimation; dropping down to the CoreGraphics level is probably the order of the day.
A simple (and possibly not the best) thing to try (no guarantees it works) is to create an imageView and round the corners until it's circular, something like this:
[imageView.layer setCornerRadius:60.0f];
[imageView.layer setMasksToBounds:YES];
[imageView.layer setBorderWidth:0.0f];
Set the initial imageView small (1px by 1px). Then use some simple animation code:
[UIView beginAnimations:nil context:NULL]; {
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDelay:2.0];
[UIView setAnimationDuration:1.0];
[UIView setAnimationDelegate:self];
// set the imageView to the size you want here
} [UIView commitAnimations];
Try this code.
Create an imageView.
UIImageView *imgv = [[UIImageView alloc]initWithFrame:
CGRectMake(0, 0, 0, 0)];
imgv.image = [UIImage imageNamed:#"black.png"];
//position the imageview to the center of the screen.
imgv.center = self.view.center;
[self.view addSubview:imgv];
[imgv release];
//now give the imageview a new frame of full size with an animation
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
//i am assuming that your full frame is 320x480
imgv.frame = CGRectMake(0, 0, 320, 480);
[UIView commitAnimations];
Now the imageview will appear to be scaling from the center of the screen to fill it.
I am trying to create an animation effect where an image who's alpha value is 0.5 (approximately) at start changes to 1 gradually, but not like the conventional effect. Here is what I am looking for.
original Image.
after some time
after some more time
and at the end
So basically if the alpha value of part of the image can be reduced than I can show the animation of filling up a glass or something close to it.
I don't know if this can be done at all or if it's possible with core graphics or core animation.
P.S. They are made in photoshop.
Does anyone have any thoughts about what should be done to achieve this kind of animation?
I'd suggest the following:
Create the UIImageView that shows the image in the view.
Create a new UIView with a background color and transparency:
UIView *transpView = [UIView new];
tranpsView.alpha = 0.4;
transpView.frame = self.view.frame;
[self.view addSubview:transpView];
Animate the transparent view to slide upwards:
[UIView animateWithDuration:1.0 animations:^{
transpView.frame = CGRectOffset(transpView.frame, 0, -tranpsView.frame.size.height);
}];
Don't forget to release the tranpsView after you are done. Possibly with the completed block.
To get the "glass filling up" type of animation, just change the frame of of the tranpsView accordingly. The tranpsView can be any UIView subclass (like a texture of liquid or something).
Hope that helps.!
You can add view above and change it's frame. The best way.
If you're using images, then sunclass UIView and implement
-(void) drawRect:(CGRect) rect {
//if you want to clip the image, the frame for the view must be smaller then rect
[image drawInRect: rect];
}
Here's yet another idea.
Set the background of your UIView *view to the image.png you want with alpha = 0.5. Let
w = view.bounds.size.width;
h = view.bounds.size.height;
Create another UIView *glassView also with alpha = 0.5, and add it as a subview:
[view addSubview:glassView];
glassView.frame = CGRectMake(0.0, h, w, 0.0);
Create a UIImageView *glassImageView and set its image to image.png as well, and add it as a subview of glassView:
[glassView addSubview:glassImageView];
glassImageView.frame = CGRectMake(0.0, -h, w, h);
Now set up the animation to increase the height and alpha of glassView while maintaining the size of imageGlassView, something like this:
[UIView beginAnimations:nil context:NULL]; {
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:1.0];
[UIView setAnimationDelegate:self];
glassView.alpha = 1.0;
glassView.frame = CGRectMake(0.0, 0.0, w, h);
glassImageView.frame = CGRectMake(0.0, 0.0, w, h);
} [UIView commitAnimations];
To be fair, I haven't tested this out, but something along these lines seems like it may work. Then again, maybe not. If you found a perfect solution, please post it back. I'd love to know how to do this!
See if this helps
http://www.iphonedevsdk.com/forum/iphone-sdk-development-advanced-discussion/32481-animating-uiimage-mask-fill.html
Well I know this question is quite old and I am the one who asked it, but today I was experimenting with something similar and found this simple solution to my own question which was asked 10 months ago.
I was working on a project which requires a image to look like its loading from completely white which is 0% progress to completely red image which is 100% progress.
I thought of adding 2 imageViews containing the images on top of each other and set the content mode of the top imageview containing image with red color to bottom so that when you reduce the size of imageview the image appears to be loading and it worked. With some animation blocks the image appears to be loading. Then I thought of this question that I had asked so long and then came up with the solution to my own problem.
So the solution to this question will be that if you add 2 imageViews on top of each other and then reduce the alpha value one that is behind and reduce the height to zero and then gradually increase the height and change the y coordinate of the image view that has alpha value 1 then It will appear to be loading as asked in this question.
I have used these two functions to get the illusion of loading image.
- (void)startAnimation
{
CGRect rect = loadedImageView.frame;
rect.origin.y = unloadedImageView.frame.origin.y;
rect.size.height = unloadedImageView.frame.size.height;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:2.0f];
[UIView setAnimationDidStopSelector:#selector(stopAnimation)];
loadedImageView.frame = rect;
[UIView commitAnimations];
}
- (void)stopAnimation
{
CGRect rect = loadedImageView.frame;
rect.origin.y = unloadedImageView.frame.size.height + unloadedImageView.frame.origin.y;
rect.size.height = 0;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:2.0f];
[UIView setAnimationDidStopSelector:#selector(startAnimation)];
loadedImageView.frame = rect;
[UIView commitAnimations];
}
i'm developping an application for kids (memory game): turning two same tiles(images) and you can remove them. the problem is how to turn the uiimages with animation and only the uiimages without turning the view that contain them..
do i have to put every uiimage in a small view?
any ideas?
PS :i'm not using OpenGL
Sorry, misread the first time. Use UIImageViews for each tile and then animate as below.
a UIView is just a view, so you can animate it the way you animate any view. Its fairly straightforward.
UIImageView *tileToFlip = self.[tiles objectAtIndex:3];
CGRect frameOfTileToFlip = tileToFlip.frame;
UIImageView *newImageToShow = [[UIImageView alloc] initWithFrame:frameOfTileToFlip];
// add the image to newImageToShow
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration: 1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:newImageToShow cache:NO];
[self.View addSubview:newImageToShow]; // I'm not sure if this is necessary
[UIView commitAnimations];
[tileToFlip removeFromSuperView]; // remove it so you can add it back later
Alternatively, you can use CATransition which gives you a little more control and different transitions.
You could use UIImageView for each tile.
I want to achieve the effect of fade in / fade out the rect or text. I call CGContextFillRect or CGContextShowText in my UIVIew's drawRect: method. I wonder if there is a way to achieve the animation without using UIView' support(i.e. [UIView beginAnimations::]. The desired effect I want to achieve is similar to what in Microsoft's bing serach engine, like those small black squares fade in/ fade out as you move around the web page. Thanks in advance!
Why do you not want to use UIView's animation blocks? Animating a change in opacity of a view (UILabel or otherwise) with that is pretty easy. For example, the following code will fade out a given view over a duration of 0.5 seconds:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5f];
viewToFadeOut.alpha = 0.0f;
[UIView commitAnimations];
To fade in, simply replace the alpha value of 0.0f with 1.0f.
You can do the same using a manually constructed CABasicAnimation, manipulating the UIView's layer:
CABasicAnimation *fadeOutAnimation = [CABasicAnimation animationWithKeyPath:#"opacity"];
fadeOutAnimation.duration = 0.5f;
fadeOutAnimation.removedOnCompletion = NO;
fadeOutAnimation.fillMode = kCAFillModeForwards;
fadeOutAnimation.toValue = [NSNumber numberWithFloat:0.0f];
[viewToFadeOut.layer addAnimation:fadeOutAnimation forKey:#"animateOpacity"];
If all you want to do is fade in / out a border around a view, try animating the borderColor property of the UIView's layer (same as the above CABasicAnimation, only replacing opacity with borderColor and the toValue with a CGColor cast to id).
I have a nice and easy "zoom" animation for a view, which starts as a dot and animates up to full screen size:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:1.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
myView.frame = CGRectMake(0,0,320,480);
myView.transform = CGAffineTransformIdentity;
[UIView commitAnimations];
So far so good :-)
The problem is that when I add subviews to myView, to my surprise, they do not follow their superview's animation scheme!?!?
Btw. subviews are currently added as usual in MyView's initWithFrame. I tried to set their transform property to CGAffineTransformIdentity bu it did not help.
So, what needs to be done to allow a subview of myView to also animate in a nice "zoom" fashion together with its superview?
Thanks in advance!
/John
I just ran in to the same problem, and the solution was surprisingly easy. Whereas modifying the frame size only affects the current view, not the subviews (as you noticed), the transform property applies to the subviews as well.
I'm trying to do the reverse of what you're doing (have a subview that appears to 'drop' on to the top of an existing view when displayed, rather than zoom from the center). This code works for me:
self.transform = CGAffineTransformMakeScale(2,2);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.25];
self.transform = CGAffineTransformMakeScale(1,1);
self.alpha = 1.0;
[UIView commitAnimations];
Try setting self.transform to CGAffineTransformMakeScale(0,0) before beginning the animation, and set it back to (1,1) before committing. Don't modify the frame at all - leave it at the size you want the view to have after the animation completes.
You can always use the auto-sizing in the interface builder . This way you can specify the left/margin which you don't want to change and the axis along which you want the particular sub-view to resize to .