Opening a curtain: Animation with Core Animation - iphone

I would like to animate a curtain, which gets opened. I have two images: one for the left and one for the right side of the curtain (depicted in red). I would like to smoothly slide them away with Core Animation. For what animation type should I look for? How do I achieve a realistic sliding style?
Regards,
Stefan
alt text http://img.skitch.com/20100627-8ytxrbe64ccbruj49c2pbs7kt2.png

I'm not sure why people are suggesting using a translation. If all you need to do is slide the images, simply call -setCenter on each image view inside an animation block. Like this:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
[leftCurtainImageView setCenter:pointOffScreenLeft];
[rightCurtainImageView setCenter:pointOffScreenRight];
[UIView commitAnimations];
Where pointOffScreenLeft, and pointOffScreenRight are calculated something like:
CGPoint pointOffScreenLeft = CGPointMake(
-[leftCurtainImageView bounds].size.width,
[leftCurtainImageView frame].origin.y);
CGPoint pointOffScreenRight = CGPointMake(
[rightCurtainImageView frame].origin.x +
[rightCurtainImageView bounds].size.width,
[leftCurtainImageView frame].origin.y);
These calculations assume that the curtains are positioned at the far left and far right edges respectively of their containing view.

The easiest solution would be to have to imageview or CGLayers and then use CGAffineTransformTranslate in an animation block to slide them off screen.

Man
After a long search. The only way I could find is this.
https://github.com/Ciechan/BCMeshTransformView

Related

Animate fade-out for drawings

I have a view where the user can draw. What I want is that, the strokes that were drawn by the user must slowly fade-out in the order that they were drawn. Any advice ?
JYou can try pluging in a AnimationFadeOut and also set a duration to it. Here is a code snippet for that. Let me know if this helps you out.
-(void)fadeOut:(UIView*)viewToDissolve withDuration:(NSTimeInterval)duration andWait:(NSTimeInterval)wait
{
[UIView beginAnimations: #"Fade Out" context:nil];
// wait for time before begin
[UIView setAnimationDelay:wait];
// druation of animation
[UIView setAnimationDuration:duration];
viewToDissolve.alpha = 0.0;
[UIView commitAnimations];
}
Rather than uiview you can use your action to fade out.
For what you're asking, you could simply add a stroke as a subview, then use a cross fade transition.
[drawingView addSubView:strokeView];
[UIView transitionWithView:strokeView duration:0.2f options:UIViewAnimationOptionTransitionCrossDissolve animations:NULL completion:NULL];
For something like this (showing a drawing being completed), I would personally capture the touch points and time offsets to animate the drawing of each point at the speed of the drawer. More code, but a stylistic choice.

CGAffineTransform and scaling to center of the image

I started studying objective-c using the iPhone and iPad apps for Absolute Beginners by Rory Lewis book but i got stuck on the 5th chapter.
I want to make a button that shrinks an image.
My problem is, that after I wrote all the code, the image shrinks to the point (0, 0) of the UIImageView (the top left), while in the video the image shrinks to its center. I've done some research and found out that CGAffineTransform uses the center of the object to make translations, rotations etc.
So why does it not work in my case?
I have the latest Xcode version and haven't done anything strange.
I hope I've been clear. Sorry for my English.
EDIT
Sorry for the code, but i wrote the question from my phone.
Anyway the incriminated part goes something like this
- (IBAction)shrink:(id)sender {
if(hasShrink == NO){
[shrinkButton setTitle:#"Grow" forState:UIControlStateNormal];
}
else if(hasShrink == YES){
[shrinkButton setTitle:#"Shrink" forState:UIControlStateNormal];
}
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
myIcon.transform = CGAffineTransformMakeScale(.25, .25);
hasShrink = YES;
[UIView commitAnimations];
}
All the animations are correctly written and the app does work, it just shrinks to the top left. Why is the point not set to the center of the UIImageView by default like it should be?
EDIT:
I figured out it was a problem caused by AutoLayout.
Once disabled everything went smooth ;)
If you are transforming a view mangled managed by auto-layout, you may experience some strange side-effects. See this answer for more details.
The solution I ended up employing was to encapsulate the view I wanted to transform inside another view of exactly the same size. The outer view had the appropriate layout constraints applied to it while the inner view was simply centered in its container. Applying a CGAffineTransform scale transform to the inner view now centers properly.
Old question... but just in case others come looking:
The CGAffineTransform acts (rotates, scales) around an anchorPoint.
If you are sizing to 0, 0 then your anchor point is set to 0, 0. If you want it to size to a different point, such as the center of the image, you need to change the anchor point.
The anchor is part of a layer
So if you have a UIImageView called imageview, you would make a call like this to set the anchor to the center of imageview:
[imageview.layer setAnchorPoint:CGPointMake(0.5, 0.5)];
Try something like this:
CGAffineTransform t = CGAffineTransformMakeScale(0.5, 0.5);
CGPoint center = imageView.center; // or any point you want
[UIView animateWithDuration:0.5
animations:^{
imageView.transform = t;
imageView.center = center;
}
completion:^(BOOL finished) {
/* do something next */
}];
Next time show your code. It will be easier to help you.
Check this project: https://github.com/djromero/StackOverflow/archive/Q-13706255.zip
You must study how autolayout and autoresize affect your views. In the project above both are disabled to let you see how it works.

Touch issue during UIView Animation

I am animating UIView when user touches on custom dropdown to show picker View from bottom side.UIView contains Pickerview...so when I change it's frame to move upwards.I get what I want, but pickerView doesn't recognize touch !(see screenshot below)
code is like this
CGRect pickerFrame=self.pickerSheet.frame;
CGRect viewFrame=self.view.frame;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:SHEET_ANIMATION_DURATION];
//change Frame of View containing UIPickerView
pickerFrame.origin.y=202+animatedDistance;
viewFrame.origin.y-=animatedDistance;//animated distance is value by which view needs to move upward.
[self.pickerSheet setFrame:pickerFrame];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
screenshot
first of all, ++please**, stop using this animation declaration, and bedin use new, block animation:
[UIView animateWithDuration:SHEET_ANIMATION_DURATION
delay:0
options:nil
animations:^{
pickerFrame.origin.y=202+animatedDistance;
viewFrame.origin.y-=animatedDistance;
}
completion:^(BOO f){
//any actions after animation ended
}];
Second, I think your problem is somewhere else. May by, you implementing your touches or picker wrong.
Please start using the new , better animation block , but I think there is something missing in your code , you need to put the code below :
UIViewAnimationOptionAllowUserInteraction
Put this code in options in the new animation block , or find a way to put it in the old animation block

Simulate push view animation inside UIView

I have a single UIView with some labels that display attributes from objects inside an array. I'm using gesture recognizers to change the current element in the array and hence the text in the labels. My question is how do I mimic a push animation without actually pushing a new view controller. Thanks for your help.
Thats simple way to use CGAffineTransformMakeTranslation to move any view from X axis to Y axis using UIViewAnimation. Before using this transition set your object frame to some x,y axis(0,0) and pass your x and y to move from.
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.4];
CGAffineTransform transform = CGAffineTransformMakeTranslation(x,y);
YOUR ViewOBJECT.transform=transform;
[UIView commitAnimations];
Have you tried to use the UIView class methods for animation? For example this could help:
+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations
In the animations block you could move one view out and another in.

Animate size of a UIView around center point

I have a UIView which I want to grow from it's center when a user touches it. The problem is that when animating it the view expands left and then moves to the right, whereas I want it to expand to the left and right, while keeping the center point the same.
This is the code I have at the moment:
[UIView animateWithDuration:1 animations:^(void) {
[view setFrame:CGRectMake(-10, 0, 320, view.frame.size.height)];
}];
I didn't think it was going to be difficult to do this, but it seems it is. Short of animating it manually with a timer I have no idea how to get it to expand from it's center.
I am not quite sure why it's working for you in a weird manner. Did you alter the anchorPoint property in any way? Otherwise it should grow from center.
Does doing
CGRect newFrame;
newFrame = CGRectInset(view.frame, -10, -30);
[UIView animateWithDuration:1 animations:^(void) {
view.frame = newFrame;
}];
also give you the same result? What about this?
[UIView animateWithDuration:1 animations:^(void) {
view.transform = CGAffineTransformScale(view.transform, 1.1, 1.1);
}];
Try the code given by #Deepak Danduprolu and also don't forgot to UnCheck the "Use AutoLayout" checkbox in the show file inspector window of the storyboard.
It works for me.
Just offset the X,Y position of the view. Lets say you're DECREASING both the width and height of the frame by 20 points: you should then ADD 10 points to both the X and Y position of the frame.