Animate array of image and textview together in UIView - iphone

I want to aniate array of imageview and textview together in uiview.
Right now i am animating image view separate and textview separate which is not looking that good.
UIView *baseView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 430)];
baseView.backgroundColor = [UIColor colorWithRed:255.0/255.0 green:252.0/255.0 blue:199.0/255.0 alpha:1.0];
[self.view addSubview:baseView];
self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(60, 30, 200, 200)];
self.imageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"1.png"],
[UIImage imageNamed:#"2.png"],
[UIImage imageNamed:#"3.png"],
[UIImage imageNamed:#"4.png"],
[UIImage imageNamed:#"5.png"],
nil];
// all frames will execute in 25 seconds
self.imageView.animationDuration = 20;
[self.imageView startAnimating];
[baseView addSubview:self.imageView];
self.textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 250, 300, 200)];
self.textView.textColor = [UIColor brownColor];
myArray = [[NSMutableArray alloc] initWithObjects:#"string1", #"string2", #"string3", #"string4", #"string5", ...., nil];
[NSTimer scheduledTimerWithTimeInterval:2.5
target:self
selector:#selector(updateText:)
userInfo:nil
repeats:YES];
[baseView addSubview: self.textView];
- (void)updateText:(NSTimer *)theTimer
{
if (index < [myArray count])
{
self.textView.text = [self.myArray objectAtIndex:index];
index++;
}
else
index = 0;
[timer invalidate];
}
How i can animate both image view and textview together within UIView.
Thanks for help.

How about this,,
in your .h
NSArray *imgsArray;
NSArray *myArray;
int indx;
NSTimer *timer;
and start your animation
-(void)start{
imgsArray = [NSArray arrayWithObjects:[UIImage imageNamed:#"1.png"],[UIImage imageNamed:#"2.png"],
[UIImage imageNamed:#"3.png"],[UIImage imageNamed:#"4.png"],
[UIImage imageNamed:#"5.png"],nil];
myArray = [[NSMutableArray alloc] initWithObjects:#"string1", #"string2", #"string3", #"string4", #"string5", nil];
timer = [NSTimer scheduledTimerWithTimeInterval:2.5
target:self
selector:#selector(updateText:)
userInfo:nil
repeats:YES];
}
- (void)updateText:(NSTimer *)theTimer
{
if (indx < myArray.count)
{
self.textView.text = [self.myArray objectAtIndex:indx];
self.imageView.image = [self.imgsArray objectAtIndex:indx];
indx++;
}
else
indx = 0;
}

Related

UIImage to slide downwards using animation

How can implement image sliding down animation in ios app?
I've used this code:
imageview.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"img10.png"],
[UIImage imageNamed:#"img11.png"],[UIImage imageNamed:#"img12.png"],[UIImage imageNamed:#"img13.png"],nil];
imageview.animationDuration = 10.00;
imageview.animationRepeatCount = 0;
[imageview startAnimating];
But this is only a simple slide show.
Help me.
try with this code..
imageview = [[UIImageView alloc] initWithFrame:self.view.frame];
imageview.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"img10.png"],
[UIImage imageNamed:#"img11.png"],[UIImage imageNamed:#"img12.png"],[UIImage imageNamed:#"img13.png"],nil];
imageview.animationDuration = 1.25;
imageview.animationRepeatCount = 10;
[imageview startAnimating];
[self.view addSubview:animationView];
[imageview release];
Please try following code
-(void) createNewImage {
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,460)];
imageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"img10.png"],
[UIImage imageNamed:#"img11.png"],
[UIImage imageNamed:#"img12.png"],
[UIImage imageNamed:#"img13.png"],nil];
[imageView setAnimationRepeatCount:10];
imageView.animationDuration =1.5;
[imageView startAnimating];
[NSTimer scheduledTimerWithTimeInterval:15.0 target:self selector:#selector(setLastImage:) userInfo:nil repeats:NO];
}
-(void)setLastImage:(id)obj
{
[imageView performSelectorOnMainThread:#selector(setImage:) withObject:[UIImage imageNamed:#"img12.png"] waitUntilDone:YES];
}
You can change the centre of UIImageView using imgVw.centre, you can update the centre with NSTimer. Vary the centre according to the screen, you can get screen height from
[[UIScreen mainScreen]bounds].size.height
theTimer = [NSTimer scheduledTimerWithTimeInterval:1.0f
target:self
selector:#selector(updateTimer:)
userInfo:nil
repeats:YES];
- (void)updateTimer:(NSTimer *)theTimer {
//update centre;
imgVw.centre.y += 4;
// you can update the image also if needed use if case
imgVw.image = image2;
}

How to Save only that image which is long pressed and selected by user

Currently it is saving all images but i want to save only the one which is long pressed not all images in one shot only the one user selects
- (void)viewDidLoad
{ self.view.backgroundColor = [UIColor blackColor];
UIScrollView *imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
imageScrollView.pagingEnabled = YES;
NSInteger numberOfViews = 61;
for (int i = 0; i < numberOfViews; i++) {
CGFloat xOrigin = i * self.view.frame.size.width;
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton addTarget:self action:#selector(dismissView:) forControlEvents:UIControlEventTouchUpInside];
myButton.frame = CGRectMake(xOrigin, 10, 60, 35);
//[myButton addGestureRecognizer:tap];
[myButton.layer setMasksToBounds:YES];
[myButton.layer setCornerRadius:10.0f];
myButton.layer.borderWidth = 2;
myButton.layer.borderColor = [[UIColor whiteColor] CGColor];
[myButton setTitle:#"Done" forState:UIControlStateNormal];
myButton.backgroundColor = [UIColor clearColor];
NSString *imageName = [NSString stringWithFormat:#"image%d.png", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
_imageView.tag = 128;
imageView.frame = CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height);
UILongPressGestureRecognizer *gestureRecognizer = [[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:#selector(handleLongPress:)];
//imageScrollView.userInteractionEnabled = YES;
[imageScrollView addGestureRecognizer:gestureRecognizer];
gestureRecognizer.delegate = self;
[gestureRecognizer release];
[imageScrollView addSubview:imageView];
[imageScrollView addSubview:myButton];
//[imageView release];
}
imageScrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
[self.view addSubview:imageScrollView];
[imageScrollView release];
}
- (void)handleLongPress:(UILongPressGestureRecognizer*)gestureRecognizer{
if (gestureRecognizer.state == UIGestureRecognizerStateBegan){
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Save Photo", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
[actionSheet showInView:self.view];
[actionSheet release];
}
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
NSInteger numberOfViews = 61;
for (int i = 0; i < numberOfViews; i++) {
NSString *imageName = [NSString stringWithFormat:#"image%d.png", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
UIImageWriteToSavedPhotosAlbum(imageView.image, self, #selector(image: didFinishSavingWithError:contextInfo:), nil);
}
}
How to code for that
Thanks for help
In the handleLongPress: function, use this:
UIGestureRecognizer *gestureRecognizer;
CGPoint gesturePoint = [gestureRecognizer locationInView:self.view];
Then check all of your buttons using a for loop like you have there. Use this to check if it is the pressed image:
if(CGRectContainsPoint(myButton.frame,gesturePoint)){
//save the image stored in myButton
//break will exit the for loop early, so if the view comes early in the list,
//it doesn't have to check the rest.
break;
}

delay imageviews xcode

I hit the play button 6 image views will be filled with the pressed images, I want it to view 1, wait 1 second,view2, wait one second etc.
which code should I be adding to:
[imageview7 setImage:imageview1HG.image];
[imageview8 setImage:imageview2HG.image];
[imageview9 setImage:imageview3HG.image];
[imageview10 setImage:imageview4HG.image];
[imageview11 setImage:imageview5HG.image];
[imageview12 setImage:imageview6HG.image];
I tried this: but it doesn't work:
[self performSelector:#selector(GameViewController) withObject:nil afterDelay:1];
hope someone knows bye thanks
EDIT explaining my code more
-(IBAction)play {
[self performSelector:#selector(delay) withObject:nil afterDelay:5.0];
play.enabled=NO;
UIImage *img = [UIImage imageNamed:#""];
if (imageview7.image == nil) {
if([textview.text isEqualToString:labelsText.text]){
-(void)delay {
[imageview7 setImage:imageview1HG.image];
[imageview8 setImage:imageview2HG.image];
[imageview9 setImage:imageview3HG.image];
[imageview10 setImage:imageview4HG.image];
[imageview11 setImage:imageview5HG.image];
[imageview12 setImage:imageview6HG.image];}
}
else {
-(void)delay {
[imageview7 setImage:imageview1H.image];
[imageview8 setImage:imageview2H.image];
[imageview9 setImage:imageview3H.image];
[imageview10 setImage:imageview4H.image];
[imageview11 setImage:imageview5H.image];
[imageview12 setImage:imageview6H.image];}
I guess you want to setImage on UIImageView one after the other after a delay.Here is the code for it.
Definition
#interface ViewController : UIViewController
{
NSMutableArray * imageViews;
NSMutableArray * images;
int count;
NSTimer * timer1;
}
-(void)setImageForView:(UIImageView *)imageView withImage:(UIImage *)image;
-(void)update;
-(IBAction)setImageOnViewOneAfterOther;
#end
Definition
-(void)setImageForView:(UIImageView *)imageView withImage:(UIImage *)image{
[imageView setImage:image];
}
-(void)update{
if (count < [imageViews count]) {
[self setImageForView:[imageViews objectAtIndex:count] withImage:[images objectAtIndex:count]];
count ++;
}else{
[timer1 invalidate];
}
}
-(IBAction)setImageOnViewOneAfterOther{
count = 0;
imageViews = [[NSMutableArray alloc] initWithObjects:img1v,img2v,img3v,img4v,img5v,img6v, nil]; // Set of all UIImageViews
images = [[NSMutableArray alloc] initWithObjects:img1,img2,img3,img4,img5,img6, nil];//Set of all UIImage's where img = [UIImage imageNamed:#"someImage1.png"];
timer1 = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(update) userInfo:nil repeats:YES];
}
Why not animate it?
UIImageView* animatedImageView = [[UIImageView alloc]init];
animatedImageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"1.gif"],
[UIImage imageNamed:#"2.gif"],
[UIImage imageNamed:#"3.gif"],
[UIImage imageNamed:#"4.gif"], nil]; //etc
animatedImageView.animationDuration = float([animatedImageView.animationImages count]);
animatedImageView.animationRepeatCount = 0;
[animatedImageView startAnimating];
[self.view addSubview: animatedImageView];

how to stop the animation at the last frame?

How I can stop animation on the last frame.
Code here:
- (void)method{
NSMutableArray *images = [[NSMutableArray alloc]init];
NSInteger i;
for (i = 0; i < 75; i++){
NSString *str = [NSString stringWithFormat:#"pictures%i.png", i];
UIImage *img =[UIImage imageNamed:str];
[images addObject:img];
}
// Begin the animation
somePicture = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"8Ball_003_00075.png"]];
//somePicture = [[[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 480.0f)] autorelease];
//[somePicture isAnimating]==0;
somePicture.frame = CGRectMake(0, 0, 320, 480);
[somePicture setAnimationImages:images];
somePicture.animationDuration = 5.0f;
somePicture.animationRepeatCount = 1;
[somePicture startAnimating];
somePicture.center = CGPointMake(160.0f, 230.0f);
//[images release];
[NSTimer scheduledTimerWithTimeInterval: 0.5f target: self selector: #selector(tick) userInfo: nil repeats: YES];
[self.view addSubview:somePicture];
}
-(void)tick{
if(![somePicture isAnimating]) { [timer invalidate]; [somePicture stopAnimating]; timer=nil; NSLog(#"#ye1111eah!!!");}
else
NSLog(#"#y");}
I know that I must used NSTimer but i don't know how :(
Why do you allocate somePicture twice
somePicture = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"8Ball_003_00075.png"]];
somePicture = [[[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 480.0f)] autorelease];
Can't you just use this
somePicture = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"8Ball_003_00075.png"]];
somePicture.frame = CGRectMake(0.0f, 0.0f, 320.0f, 480.0f);
UPDATE
I think this will do it
- (void)method{
NSMutableArray *images = [[NSMutableArray alloc]init];
NSInteger i;
for (i = 0; i < 75; i++){
NSString *str = [NSString stringWithFormat:#"pictures%i.png", i];
UIImage *img =[UIImage imageNamed:str];
[images addObject:img];
}
// Begin the animation
somePicture = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"8Ball_003_00075.png"]];
somePicture.frame = CGRectMake(0.0f, 0.0f, 320.0f, 480.0f);
self.view addSubview:somePicture];
//[somePicture isAnimating]==0;
[somePicture setAnimationImages:images];
somePicture.animationDuration = 5.0f;
somePicture.animationRepeatCount = 1;
somePicture.center = CGPointMake(160.0f, 230.0f);
[somePicture startAnimating];
[self performSelector:#selector(tick) withObject:self afterDelay:5.0];
}
- (void)tick
{
[somePicture stopAnimating];
if(![somePicture isAnimating])
{
NSLog(#"#ye1111eah!!!");
}
else
NSLog(#"#y");
}
hope this helps

How can i create a UIImage from a UIImageView?

I am trying to implement an animation set of images on my application.
NSArray *myImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"myImage1.png"],
[UIImage imageNamed:#"myImage2.png"],
[UIImage imageNamed:#"myImage3.png"],
[UIImage imageNamed:#"myImage4.gif"],
nil];
UIImageView *myAnimatedView = [UIImageView alloc];
[myAnimatedView initWithFrame:[self bounds]];
myAnimatedView.animationImages = myImages;
myAnimatedView.animationDuration = 0.25; // seconds
myAnimatedView.animationRepeatCount = 0; // 0 = loops forever
[myAnimatedView startAnimating];
[self addSubview:myAnimatedView];
[myAnimatedView release];
Ok, but the problem is that i am using an API function to place a marker on a map.
That function is the following:
RMMarker *newMarker;
UIImage *blueMarkerImage = [UIImage imageNamed:#"marker.png"];
newMarker = [[RMMarker alloc] initWithUIImage:blueMarkerImage anchorPoint:CGPointMake(0.5, 1.0)];
[mapView.contents.markerManager addMarker:newMarker AtLatLong:position];
[newMarker release];
The problem is that "initWithUIImage:" only works with UIImage, not with UIImageView.
So, how can i solve it??
UPDATE: My new code that is not working:
NSArray *myImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"marker0.png"],
[UIImage imageNamed:#"marker1.png"],
[UIImage imageNamed:#"marker2.png"],
[UIImage imageNamed:#"marker3.png"],
[UIImage imageNamed:#"marker4.png"],
nil];
UIImageView *myAnimatedView = [UIImageView alloc];
myAnimatedView.animationImages = myImages;
myAnimatedView.animationDuration = 10; // seconds
myAnimatedView.animationRepeatCount = 1; // 0 = loops forever
[myAnimatedView startAnimating];
RMMarker *newMarker = [[RMMarker alloc] initWithUIImage:[myAnimatedView image] anchorPoint:CGPointMake(0.5, 1.0)];
[mapView.contents.markerManager addMarker:newMarker AtLatLong:markerPosition];
You can try using image message from UIImageView:
newMarker =
[
[RMMarker alloc]
initWithUIImage:[myAnimatedView image]
anchorPoint:CGPointMake(0.5, 1.0)
];
Or, if what you need is creating a UIImageView then you can try:
UIImageView *newMakerImageView =
[[UIImageView alloc] initWithImage:
[
[RMMarker alloc]
initWithUIImage:yourImageHere
anchorPoint:CGPointMake(0.5, 1.0)
]
];
UIImageView *myAnimatedView = [UIImageView alloc]
That may just be a copy/paste error, but you forget the init.
UIImageView *myAnimatedView = [[[UIImageView alloc] initWithFrame:[self bounds]] autorelease]