"reveal" an image on iphone - iphone

i would like to reveal an image on an UIView:
showing an empty screen
slowly reveal the image from to to bottom
Any ideas ?

Something like this:
UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"someImage.png"]];
CGRect endFrame = img.frame;
CGRect startFrame = img.frame;
startFrame.size.height = 0;
img.frame = startFrame;
[view addSubview:img];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration: 0.5];
img.frame = endFrame;
[UIView commitAnimations];

Related

Set animation for a dynamically created UIView

I figured out most of the thing how to add UIView dynamically, but now I'm stuck trying to create a alpha animation when a uibutton is clicked and the uiview is shown up. Not sure what I am missing here:
- (void)buttonPressedAction:(id)sender
{
CGRect frame;
frame.origin.x = 0;
frame.origin.y = 0;
frame.size = self.scrIcon.frame.size;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
UIView* view = [[UIView alloc] initWithFrame:frame];
view.alpha = 1.0;
[UIView commitAnimations];
view.backgroundColor = [UIColor redColor];
[scrIcon addSubview:view];
/*
UIStoryboard* sb = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:[NSBundle mainBundle]];
CategoryViewController* theView = [sb instantiateViewControllerWithIdentifier:#"categoryIdentifier"];
theView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:theView animated:YES];*/
}
scrIcon is a UIScrollView
thanks
You can try this method: animateWithDuration:animations:
UIView *view = [[UIView alloc] initWithFrame:self.scrIcon.frame];
view.backgroundColor = [UIColor redColor];
view.alpha = 0.0;
[scrIcon addSubview:view];
[UIView animateWithDuration:1.0 animations:^{
view.alpha = 1.0;
}];
Modified your code add the view 1st to scrIcon. Then perform the animation.
- (void)buttonPressedAction:(id)sender
{
CGRect frame;
frame.origin.x = 0;
frame.origin.y = 0;
frame.size = self.scrIcon.frame.size;
UIView* view = [[UIView alloc] initWithFrame:frame];
view.alpha = 0.0f;
view.backgroundColor = [UIColor redColor];
[scrIcon addSubview:view];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
view.alpha = 1.0;
[UIView commitAnimations];
}
In ViewDidLoad: just init Your View…..
YourAnimateView = [[UIView alloc]initWithFrame:CGRectMake(-320, 0, 320, 320)];
[self.view addSubview:YourAnimateView];
In Your Button Action Method : just put this
[UIView animateWithDuration:0.5 delay:0.1
options:UIViewAnimationOptionTransitionCurlDown
animations:^{
//animation code
YourAnimateView.frame = CGRectMake(0, 0, 320, 345);
} completion:^(BOOL finished) {
//completion code
}];

Remove view 1 after replaced by view2

I have a small code to display image 1 and after 2 seconds replace image1 by image2 with animation below
UIImageView *view1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 410, 1020, 400)];
UIImage *image = [UIImage imageNamed:#"img.jpeg"];
view1.image = image;
[self.view addSubview:view1];
UIImageView *view2 = [[UIImageView alloc] init ];
view2.frame = CGRectMake(0, 410, 0, 400);
view2.image = [UIImage imageNamed:#"bien.jpeg"];
[self.view addSubview:view2];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.8];
[UIView setAnimationDelay:2];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(removeView: view1:)];
view2.frame = CGRectMake(0, 410, 800, 400);
[UIView commitAnimations];
and function removeView to remove view1 from superview below:
-(void)removeView: (UIImageView *)view1{
[view1 removeFromSuperview];
}
So i dont know why my function to remove view1 from superview not work, please help me! Thanks alot...
The selector cannot pass the parameter. Modify your method to
-(void)removeView{
[view1 removeFromSuperview];
}
where "view1" is a instance to your view.
and your selector to:
[UIView setAnimationDidStopSelector:#selector(removeView)];
I would recommend you to use block based animations (available since iOS 4).
They are much easier to use and don't need to send parameters through methods and all that stuff.
Example:
UIImageView *view1 = [[UIImageView alloc] init];
//initialize your UIImageView view1
[self.view addSubview:view1];
UIImageView *view2 = [[UIImageView alloc] init];
//initialize your UIImageView view2
[UIView animateWithDuration:2 animations:^{
//here happens the animation
[self addSubview:view2];
} completion:^(BOOL finished) {
//here happens stuff when animation is complete
[view1 removeFromSuperView];
}];
Remember to vote up and or mark as accepted answer ;)
Try this code!!
UIImageView *view1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 410, 1020, 400)];
UIImage *image = [UIImage imageNamed:#"img.jpeg"];
view1.tag = 1;
view1.image = image;
[self.view addSubview:view1];
UIImageView *view2 = [[UIImageView alloc] init ];
view2.frame = CGRectMake(0, 410, 0, 400);
view2.image = [UIImage imageNamed:#"bien.jpeg"];
[self.view addSubview:view2];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.8];
[UIView setAnimationDelay:2];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(removeView: view1:)];
view2.frame = CGRectMake(0, 410, 800, 400);
[UIView commitAnimations];
// Remove View Method
-(void)removeView : (UIImageView *) imgVew {
if (imgVew.tag == 1)
[imgVew removeFromSuperView];
}
Access it -
[UIView setAnimationDidStopSelector:#selector(removeView:)];
the problem is simple, this selector is not exists in your class: -removeView:view1:. thus there is nothing to call back after the animation finished. this is why your -(void)removeView:(UIImageView *)view1; method will be never called back.
please, realised your real selector is -removeView: and it is not equal with -removeView:view1:
if you want to pass parameter through the didStopSelector, I would have a bad news: you cannot do it as you did in your code, so this part is wrong at all:
// WRONG AT ALL:
[UIView setAnimationDidStopSelector:#selector(removeView:view1:)];
// PROPER WAY:
[UIView setAnimationDidStopSelector:#selector(animationDidStop:finished:context:)];
because the didStopSelector must be the following kind of selector with the following parameters.
- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context;
you can pass parameter for your callback method like this:
[UIView beginAnimations:nil context:view1]; // see the context's value
and in your didStopSelector you can use it somehow like:
- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
[((UIView *)context) removeFromSuperView];
}

animate and resize UIImageView

How do I animate and resize a UIImageView? I tried doing this and it didn't work out for me:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
imgView.frame = CGRectMake(0, 0, 320, 480);
[UIView commitAnimations];
Try using a block animation:
[UIView animateWithDuration:1.0f // This can be changed.
animations:^{
CGRect frame = imageView.frame;
frame.size.width += 100.0f; // This is just for the width, but you can change the origin and the height as well.
imageView.frame = frame;
}
completion:^(BOOL finished){
}];
Hope that Helps!
Try this code. This works fine for me,
CGPoint newLogoutCenter = CGPointMake(160, 364);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0f];
twitterBarVIew.center = newLogoutCenter;
[UIView commitAnimations];
If we assume that you have correct reference to the imgView, then it seems that starting frame of the imgView is the same before and after animation.
Try to set imgView.frame to:
imgView.frame = CGRectMake(100,100,100,100);
//Set up the UIImageView. the Frame determines the position and size of the image.
UIImageView *i = [[UIImageView alloc] initWithFrame:[CGRectMake(50.0,50.0,50.0,50.0)]];
//Add images to UIImageView
i.images = [[NSArray alloc] initWithObjects:[UIImage imageNamed:#"1.png"],
[UIImage imageNamed:#"2.png"], nil];
//Set the amount of time for the animation to run
i.animationDuration = 1.0f;
//start the animations
[i startAnimating];
You can resize the image by changing the frame of the image
i.frame = CGRectMake(x, y, width, height);

iphone - moving a UIImageView

I'm having difficulty moving an image to another location after the first animation is finished.
the image animates at a point that I've specified, then stops (that works fine). I would then like to move the image to another location and repeat.
here's my code:
-(void) loadTap {
NSArray *imageArray = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:#"tap1.png"],
[UIImage imageNamed:#"tap2.png"],
[UIImage imageNamed:#"tap3.png"],
[UIImage imageNamed:#"tap4.png"],
nil];
tapImage.animationImages = imageArray;
tapImage.animationRepeatCount = 1;
[imageArray release];
tapImage.animationDuration = 1;
tapImage.animationRepeatCount = 20;
[tapImage startAnimating];
tapImage.center = CGPointMake(156, 110);
}
thanks for any help.
In order to move an image, you should enclose the code to move in an animation block:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
tapImage.center = CGPointMake(156, 110);
[UIView commitAnimations];
You can also give a method to execute upon completion of the animation with UIView setAnimationDidStopSelector: method.
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(animateImages)];
tapImage.center = CGPointMake(156, 110);
[UIView commitAnimations];
//other stuff
-(void)animateImages{
[tapImage startAnimating];
}

animate UIImageView with flip animation

I'm a noob in objective-c and cocoa, and I would like to implement a simple animation of UIImageView with flipping animation. But I'm having a hard time doing it. Below are my snippets:
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0,0,33,33)];
UIImageView *img1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:IMG1]];
UIImageView *img2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:IMG2]];
[containerView addSubview:img1];
[containerView addSubview:img2];
[self.view addSubview:containerView];
CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationDuration:0.75];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:containerView cache:YES];
[containerView exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
[UIView commitAnimations];
Thanks.
You refer to an object called view which is never declared. I think you need to change your second-to-last line to
[containerView exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
See if that helps.