How to move an array of images randomly in iphone app - iphone

How can I move an array of images randomly in iPhone app.
Bellow code is for moving one image. I've to use an array of images.
- (void)moveImage:(UIImageView *)image duration:(NSTimeInterval)duration
curve:(int)curve x:(CGFloat)x y:(CGFloat)y
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:duration];
[UIView setAnimationCurve:curve];
[UIView setAnimationBeginsFromCurrentState:YES];
// The transform matrix
CGAffineTransform transform = CGAffineTransformMakeTranslation(x, y);
image.transform = transform;
// Commit the changes
[UIView commitAnimations];
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIImageView *imageToMove =
[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"fish.png"]];
imageToMove.frame = CGRectMake(70, 120, 100, 100);
[self.view addSubview:imageToMove];
[self moveImage:imageToMove duration:1.0
curve:UIViewAnimationCurveLinear x:0.0 y:110.0];
}

you can use this it might help you:
NSArray *animationArray=[NSArray arrayWithObjects:
[UIImage imageNamed:#"img1.png"],
[UIImage imageNamed:#"img2.png"],
[UIImage imageNamed:#"img3.png"],
[UIImage imageNamed:#"img4.png"],
nil];
UIImageView *animationView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0,320, 460)];
animationView.backgroundColor=[UIColor purpleColor];
animationView.animationImages=animationArray;
animationView.animationDuration=1.5;
animationView.animationRepeatCount=0;
[animationView startAnimating];
[self.view addSubview:animationView];
[animationView release];
This is just a conceptual code so please make changes according to your need.

Related

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];
}

How to specify a size to a UIAlert

I am trying to display a UIImage in a UIAlert.
I want to display it nearly fullscreen. For example my image has a 300x450px size.
Well I add it as a subview to my UIAlert.
But the UIAlert has default coordinates to keep it centered.
So I can add a image bigger than the UIAlert frame, but it covers the UIAlert...
I tried to specify a frame for my UIAlert, but it has no effect on it.
In fact I would like to add my image as a real content of the UIAlert, as well as simple text.
Is this possible ?
Consider writing your own Dialog popup instead of messing with the UIAlertView. If you want the 'bounce-in' animation that can easily be achieved using transform animations. Here is a simple popup dialog box I've just made.
To test it out create a new View-Based Application project and add this class. Then you can test it by adding the 'usage' code below to your *ViewController.m
This is a very simple example to demo the theory. It would make more sense to have a static method in SimpleDialog that shows the popup, but I didn't want to make the example overly complex.
SimpleDialog.h
#import <UIKit/UIKit.h>
#interface SimpleDialog : UIView
{
}
- (void) show;
#end
SimpleDialog.m
#import "SimpleDialog.h"
#define BOUNCE_SPEED 1
#implementation SimpleDialog
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
// Initialization code
self.backgroundColor = [UIColor greenColor];
UIButton* closeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
closeButton.frame = CGRectMake(0, 0, 200, 20);
closeButton.center = self.center;
[closeButton setTitle:#"Close" forState:UIControlStateNormal];
[closeButton addTarget:self action:#selector(hide) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:closeButton];
}
return self;
}
- (void) show
{
UIWindow* window = [UIApplication sharedApplication].keyWindow;
if (!window)
{
window = [[UIApplication sharedApplication].windows objectAtIndex:0];
}
[window addSubview:self];
self.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.001, 0.001);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.2*BOUNCE_SPEED];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(bounceInStopped)];
self.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.1, 1.1);
[UIView commitAnimations];
}
- (void)bounceInStopped
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.15*BOUNCE_SPEED];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(bounceOutStopped)];
self.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.9, 0.9);
[UIView commitAnimations];
}
- (void)bounceOutStopped
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.15*BOUNCE_SPEED];
self.transform = CGAffineTransformIdentity;
[UIView commitAnimations];
}
- (void) hide
{
[self removeFromSuperview];
}
#end
Usage
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton* popButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
popButton.frame = CGRectMake(0, 0, 200, 20);
popButton.center = self.view.center;
[popButton setTitle:#"Pop" forState:UIControlStateNormal];
[popButton addTarget:self action:#selector(showIt) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:popButton];
}
- (void) showIt
{
SimpleDialog* simple = [[SimpleDialog alloc] initWithFrame:CGRectMake(0, 0, 300, 400)];
simple.center = self.view.center;
[self.view addSubview:simple];
[simple show];
[simple release];
}
Yes this is possible.
But it requires to customize the UIAlertView.
Using UIAlertView customization you can add anything.

transition animation on only a part of the screen

I am displaying two images in my iPhone app, one on top, one on the bottom. They cover the entire screen. The user, with a swipe gesture, can change either of the images, depending on where the swipe started.
I want the image to change with an animated transition. It currently works without animation, or with the entire screen transitioning. Is it possible to make the transition occur over part of the screen?
I load the images for the first time (in viewDidLoad) thus:
// top image
UIImage *topImage = [UIImage imageNamed:#"top1.png"];
CGRect topframe = CGRectMake(0.0f, 0.0f, 320.0f, 240.0f);
UIImageView *topView = [[UIImageView alloc] initWithFrame:topframe];
topView.image = topImage;
[self.view addSubview:topView];
[topView release];
// bottom image
UIImage *bottomImage = [UIImage imageNamed:#"bottom1.png"];
CGRect bottomframe = CGRectMake(0.0f, 240.0f, 320.0f, 240.0f);
UIImageView *bottomView = [[UIImageView alloc] initWithFrame:bottomframe];
bottomView.image = bottomImage;
[self.view addSubview:bottomView];
[bottomView release];
When I detect a swipe, and detect which of the two images are to be changed, I call a routine like this one:
- (void)changeTopImage:(NSString *)newImage {
UIImage *topImage = [UIImage imageNamed:newImage];
CGRect topframe = CGRectMake(0.0f, 0.0f, 320.0f, 240.0f);
UIImageView *topView = [[UIImageView alloc] initWithFrame:topframe];
topView.image = topImage;
[self.view addSubview:topView];
[topView release];
}
Essentilly, I'm continually loading images on top of each other. Is that the best way to do it, especially in terms of memory management?
Everything else I've tried, using techniques such as below, make the entire screen transition:
[UIView beginAnimations:nil context:nil];
...
[UIView commitAnimations];
Thanks for any leads on which way I should be going on this.
well, an easy way could be this (repeat for the bottomImage):
don't release topView when you load it the first time in your method,
but declare it in your .h file,
and use a tempView too:
#interface YourClass: UIViewController{
UIImageView *topView;
UIImageView *tempView;
}
this way you can call them to move them and remove them when you load a new image
then in .m:
EDIT: some correction (see coco's comments):
[a] [b] line 4 = [c] line 11:
- (void)changeTopImage:(NSString *)newImage {
UIImage *topImage = [UIImage imageNamed:newImage];
//CGRect topframe = CGRectMake(0.0f, 0.0f, (320.0f + 320), 240.0f);
CGRect topframe = CGRectMake((0.0f + 320), 0.0f, 320.0f, 240.0f);
tempView = [[UIImageView alloc] initWithFrame:topframe];
//topView.image = topImage;
tempView.image = topImage;
[self.view addSubview:tempView];
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
[UIView beginAnimations:#"animation" context:NULL];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelegate:self];
tempView.center = topView.center;
// do this to push old topView away, comment next line if wanna new image just cover it
//topView.center = CGPointMake(topView.x - 320, topView.y);
topView.center = CGPointMake(topView.center.x - 320, topView.center.y);
// call a method when animation has finished:
[UIView setAnimationDidStopSelector:#selector(endOfAnimation:finished:context:)];
[UIView commitAnimations];
}
- (void)endOfAnimation:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context{
[topView removeFromSuperview];
topView = tempView;
[tempView release];
tempView = nil;
[[UIApplication sharedApplication] endIgnoringInteractionEvents];
}
- (void)dealloc {
if (tempView != nil) {
[tempView release];
}
[topView release];
[super dealloc];
}

"reveal" an image on 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];

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.