I have lot of such case statements which would fire various UIView animations. I would like to write a module and parametrize these so I don't have to repeat similar code over and over. What is the best way to pass (parametrized) values for animationRepeatCount & animationDuration?
case 10: //Animation Number 10
//.........
imageAnimation2.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"monkey_dancing_01.png"],
[UIImage imageNamed:#"monkey_dancing_02.png"],
[UIImage imageNamed:#"monkey_dancing_03.png"],
[UIImage imageNamed:#"monkey_dancing_04.png"],
nil];
imageAnimation2.animationRepeatCount = 4;
imageAnimation2.animationDuration = 2;
[imageAnimation2 startAnimating];
[self loadSoundEffectAudio:#"Monkey_10"];
[self performSelector:#selector(stopSoundEffectAudio) withObject:nil afterDelay:4];
break;
Depending on how regular your assets and animations are, you could make some utility methods to handle much of that.
Like say:
+ (void) loadAnimation:(UIImageView *)imageView name:(NSString *)filename
{
NSMutableArray * array = [[NSMutableArray alloc] init];
UIImage * image;
int index = 1;
NSString * string;
do
{
string = [NSString stringWithFormat:#"%#%04d.png", filename, index++];
image = [UIImage imageNamed:string];
if(image)
[array addObject:image];
} while (image != nil);
imageView.animationImages = array;
imageView.animationDuration = [array count] / 18;
[array release];
}
The above code loads up animations into an array with very regular naming conventions (XXXX.png). They are also all based on the same framerate so I can build the duration at the same time.
Related
I have:
Class Piece inherit UIImageView;
- (void)setJumpAt:(int)frame {
NSMutableArray *ret = [SkinConstants BallSelected];
NSString *name = [NSString stringWithFormat:#"balls-%d-%d", color - 1, [[ret objectAtIndex:frame] intValue]];
UIImage *a = [UIImage imageNamed:name];
NSLog(#"%d setJumpAt: %#", self.tag ,name);
[self performSelectorOnMainThread:#selector(setImage:) withObject:a waitUntilDone:NO];
[self setNeedsDisplay];
[self setNeedsLayout];}
Class Player contain NSMutableArray of Piece;
Class JumpThread contain NSTimer use to set image of Piece;
- (void) timer_Tick{
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
[piece setJumpAt:frame++];
[piece setNeedsDisplay];
if (frame == len)
frame = 0;
dispatch_async(dispatch_get_main_queue(), ^{
});
});}
I run code normal, but image of Piece not change in mainview,
Sorry, I'm not so good at English.
UIImageView can animate images with an array of UIImages and suitable animationDuration
//self is an UIImageView instance
NSArray images = [...]; // your frames
self.animationImages = images;
self.animationDuration = 2.0f // 2.0s
[self startAnimating];
I can't figure out how could I duplicate UIView with its contents. Or is it even possible?
I have made and UIView with a label and a button inside a .xib file. Now I wish to copy this view n times only with different label text.
I'm trying to do this like this, but such way I only get the last object shown. _view1 is IBOutlet just like _view1Label.
NSMutableArray *Info = [[NSMutableArray alloc]initWithCapacity:number.intValue];
for(int i = 0; i != number.intValue; i++)
{
NSString *number = [NSString stringWithFormat:#"Zona%i ",[[NSUserDefaults standardUserDefaults] stringForKey:#"ObjectNumber"].intValue];
NSString *zona = [NSString stringWithFormat:#"%#%i",number, i+1];
NSString *parinkta = [[NSUserDefaults standardUserDefaults] stringForKey:zona];
_view1Label.text = parinkta;
_view1.hidden = false;
CGRect newrect = CGRectMake(_view1.frame.origin.x, _view1.frame.origin.y + (80 * i), _view1.frame.size.width, _view1.frame.size.height);
_view1.frame = newrect;
[Info addObject:_view1];
}
for(int i = 0; i != number.intValue; i++)
{
UIView *add = [Info objectAtIndex:i];
[self.view addSubview:add];
}
I guess you'll get the idea what I am trying to do, maybe my idea of doing this is totaly wrong, so could anyone help me get on the path on this ?
Load the view multiple times from the nib, adjusting the label contents on each copy you load. This is much easier, shorter, less prone to error than trying to copy the UIView contents in-memory which you're trying to do.
Here's an example of using UINib to access the nib contents:
UINib *nib = [UINib nibWithNibName:#"nibName" bundle:nil];
NSArray *nibContents = [nib instantiateWithOwner:nil options:nil];
// Now nibContents contains the top level items from the nib.
// So a solitary top level UIView will be accessible
// as [nibContents objectAtIndex:0]
UIView *view = (UIView *)[nibContents objectAtIndex:0];
// assumes you've set the tag of your label to '1' in interface builder
UILabel *label = (UILabel *)[view viewWithTag:1];
label.text = #"My new text";
So just repeat the above code for each nib instance you want.
if you wish to show n views then you have to create the view n times, inside the first for loop, you can not place a single view in multiple places
If you are created the first view through code then you can use the following method.
Change your for loop like:
for(int i = 0; i != number.intValue; i++)
{
NSString *number = [NSString stringWithFormat:#"Zona%i ",[[NSUserDefaults standardUserDefaults] stringForKey:#"ObjectNumber"].intValue];
NSString *zona = [NSString stringWithFormat:#"%#%i",number, i+1];
NSString *parinkta = [[NSUserDefaults standardUserDefaults] stringForKey:zona];
UIView *tempView = [[UIView alloc] init];
UILabel *tempLabel = [[UILabel alloc] init];
tempLabel.frame = _view1Label.frame;
tempLabel.text = _view1Label.text;
[tempView addSubview: tempLabel];
tempView.frame = _view1.frame;
_view1Label.text = parinkta;
_view1.hidden = false;
CGRect newrect = CGRectMake(_view1.frame.origin.x, _view1.frame.origin.y + (80 * i), _view1.frame.size.width, _view1.frame.size.height);
_view1.frame = newrect;
[Info addObject:tempView];
}
I am beginner of iphone, I have all images in array but how to display one by one when I execute the array in a loop..
NSArray *images = [[NSArray arrayWithObjects:
[UIImage imageNamed:#"1.png"],
[UIImage imageNamed:#"2.png"],
[UIImage imageNamed:#"3.png"],
[UIImage imageNamed:#"4.png"],
nil] retain];
int i=0;
int width = 0;
int height = 0;
for ( NSString *imageName in images )
{
//NSTimer *timer=[[NSTimer alloc]init];
UIImage *image = [images objectAtIndex:i];
//UIImage *image = [UIImage imageNamed:imageName];
animalphoto = [[UIImageView alloc] initWithImage:image];
animalphoto.contentMode = UIViewContentModeScaleAspectFit;
animalphoto.clipsToBounds = YES;
animalphoto.animationDuration=5.0;
animalphoto.animationRepeatCount=0;
animalphoto.frame = CGRectMake( animalphoto.frame.size.width * i++, 0, animalphoto.frame.size.width, animalphoto.frame.size.height);
width = animalphoto.frame.size.width;
height = animalphoto.frame.size.height;
NSLog(#"print:%#",image);
[animalphoto release];
}
}
Give any suggestion and sample code which is apply in our code..
Instead of putting one image just put whole array of images to the imgview.animationImages = images without the for loop
For further reference visit this site.
you need to use UIImageView's object animationImages to set the UIImage array
set animationRepeatCount
set animationDuration
then call [imageView startAnimating]; to start the animation
I have UIScrollView that contains images from the server.
I should put Activity Indicator while the image is currently loading.
UIScrollView contains dynamic number of images from the server.
May I know how can I add activity indicators on each page while the image is loading and remove once image is loaded.
Here's my code to retrieve images:
NSDictionary *items = [NSDictionary dictionaryWithObject:dictInfo forKey:#"images"];
imageList = [items objectForKey:#"images"];
NSArray *img = [imageList objectForKey:#"list"];
NSInteger imgCount = [img count];
buttonArray = [[NSMutableArray alloc] init];
for (int i=0; i<imgCount; i++) {
NSDictionary *imgDict = [img objectAtIndex:i];
// REQUEST FOR IMAGES
NSString *imgPath = [imgDict objectForKey:#"image_slot"];
NSString *imgURL = imgPath;
__block ASIHTTPRequest *requestImage = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:imgURL]];
[requestImage setCompletionBlock:^{
imgView = [UIImage imageWithData:[requestImage responseData]];
if (imgURL.length) {
[pendingRequests removeObjectForKey:imgURL];
}
scrollView.userInteractionEnabled = YES;
scrollView.exclusiveTouch = YES;
scrollView.canCancelContentTouches = YES;
scrollView.delaysContentTouches = YES;
scrollView.bounces = NO;
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
SWTUIButton *imgBtn = [[SWTUIButton alloc] initWithFrame:frame];
imgBtn.url = [requestImage.userInfo objectForKey:#"rURL"];
[imgBtn setImage:imgView forState:UIControlStateNormal];
imgBtn.backgroundColor = [UIColor clearColor];
[imgBtn addTarget:self action:#selector(buttonpushed:) forControlEvents:UIControlEventTouchUpInside];
[scrollView addSubview:imgBtn];
[buttonArray addObject:imgBtn];
[imgBtn release];
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * img.count, self.scrollView.frame.size.height);
}];
I highly suggest you use NINetworkImageView from https://github.com/jverkoey/nimbus project.
It's very light and useful.
It has a delegate method to let you know when an image is loaded.
What you basically need to do is:
1. create an NINetworkImageView for each page, just like you do with UIImageView, and call set
NINetworkImageView* networkImageView = [[[NINetworkImageView alloc] initWithImage:initialImage]
autorelease];
networkImageView.delegate = self;
networkImageView.contentMode = UIViewContentModeCenter;
[networkImageView setPathToNetworkImage:
#"http://farm3.static.flickr.com/2484/3929945380_deef6f4962_z.jpg"
forDisplaySize: CGSizeMake(kImageDimensions, kImageDimensions)];
https://github.com/jverkoey/nimbus/tree/master/examples/photos/NetworkPhotoAlbums
the add the indicator to the networkImageView as a subview.
implement the delegate as follows:
-(void)networkImageView:(NINetworkImageView *)imageView didLoadImage:(UIImage *)image {
[imageView removeAllSubviews];
}
the end result would be a much smaller code for doing the same thing.
I have 8 UIImageView in my view, all declared in the .h and .m.
I called them img01, img02, img03,..., img08
and I also have 8 pics, named 01.png, 02.png, 03.png,..., 08.png.
if I want to set the first imageView to the first pic I'll use the following command:
UIImage *image = [UIImage imageNamed:#"01.png"];
[img01 setImage:image];
What if I have a lot of UIImageViews and pics?
I tried to use this code but couldn't figure out how to do the last line of code.
int count = 1;
while (count <= 8) {
NSString *countString = [[NSString alloc]initWithFormat:#"%i", count];
NSString *picname = [NSString stringWithFormat:#"0%#.png", countString];
NSString *imgName = [NSString stringWithFormat:#"img0%#", countString];
UIImage *image = [UIImage imageNamed:picname];
[img01 setImage:image];
count++;
}
The code will only change the first imageView. I need it to change all the imageViews.
Create a NSMutableArray, and fill it with the UIImageViews. Currently you only set the first:
[img01 setImage:image];
You can do something like that:
NSMutbaleArray imageViews = [[NSMutableArray alloc] initWithCapacity:8];
for (int i = 0; i < 8; ++i) {
NSString *countString = [NSString stringWithFormat:#"%i", count];
NSString *picname = [NSString stringWithFormat:#"0%#.png", countString];
NSString *imgName = [NSString stringWithFormat:#"img0%#", countString];
UIImage *image = [UIImage imageNamed:picname];
UIImageView * view = [[[UIImageView alloc] init] autorelease];
[view setImage:image];
[imageViews addObject:view];
}