Simple Animation in Cocoa Objective-C - cocoa

I have written a simple animation for an iOS game and I am trying to convert it for Mac OS.
Here is my code for iOS
NSMutableArray *imgArr = [[[NSMutableArray alloc] init] autorelease];
for(int i = 1; i < 6 + 1; i++) {
[imgArr addObject: [UIImage imageNamed:[NSString stringWithFormat:#"%#%d.png", filePrefix, i]]];
}
UIImageView * animation = [[UIImageView alloc] initWithFrame:rect];
animation.animationImages = imgArr;
animation.animationRepeatCount = 1;
[animation startAnimating];
[self.view addSubview:animation];
As for cocoa, this part gives errors.. How can I fix this?
//animation.animationImages = imgArr;
//animation.animationRepeatCount = 1;
//[animation startAnimating];
New code with timer
for(int i = 0; i < 6; i++)
[NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:#selector(anim)
userInfo:nil
repeats:NO];
- (void)anim {
++num;
NSImageView *animation = [[NSImageView alloc] initWithFrame:NSMakeRect(shipPosition.x, shipPosition.y, shipSize.width, shipSize.height)];
[animation setImage: [NSImage imageNamed:[NSString stringWithFormat:#"explosprite%d.png", num]] ];
[objectView addSubview:animation];
[animation release];
}

UIImageView does not exist in Cocoa at all. Cocoa does not really have support for sprite animation like you're trying to do here. The most common way AFAIK is just to set an NSTimer and update the image appropriately when the timer fires.
You could also create a custom CALayer subclass that handles animating images. I'm not sure that's necessarily easier, but it's an option.

Related

Load images on background thread in Spritekit

Right guys, I have scroll view in my SKScene running from a SKAction with the following code -
for (int i=0; i<[listOfImages count]; i++) {
NSDictionary *myDic = [listOfImages objectAtIndex:i];
NSString *urlImage = [myDic objectForKey:#"product_image"];
NSURL *imageURL = [NSURL URLWithString:urlImage];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *cacheImage = [UIImage imageWithData:imageData];
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/4+leftMargin, 0, cacheImage.size.width/2, cacheImage.size.height/2)];
[image setImage:cacheImage];
image.tag = tag;
image.contentMode = UIViewContentModeScaleAspectFit;
[scrollView addSubview:image];
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
recognizer.numberOfTapsRequired = 1;
recognizer.numberOfTouchesRequired = 1;
recognizer.delegate = self;
[image addGestureRecognizer:recognizer];
[image setUserInteractionEnabled:YES];
leftMargin += SCREEN_WIDTH;
tagValue += 1;
tag += 1;
}
I need to understand how to load the images in the background as there are almost 140 images to load and its taking 5 minutes to load everything and show. Please help me out !!
You could have a queue where one load ending triggers another, as opposed to triggering all the loads at once.
I did a google search and got this if you need help with applying the concept :
http://khanlou.com/2012/08/asynchronous-downloaded-images-with-caching/

Xcode: ScrollView & image array

I have an array with images presented in a scrollView. It works perfectly on the simulator however when i run it on the actual device (1st gen iPad) it crashes. At first i lowered the quality of the images to the lowest possible (<120kb each) but that didn't solved my problem. Then i reduced the number of images from 13 to 6 and it works. Is there any work you can think of?
Below is the code i use:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
images = [NSArray arrayWithObjects:[UIImage imageNamed:#"back1.jpg"], [UIImage imageNamed:#"back2.jpg"], [UIImage imageNamed:#"back3.jpg"], [UIImage imageNamed:#"back4.jpg"], [UIImage imageNamed:#"back5.jpg"], [UIImage imageNamed:#"back6.jpg"], [UIImage imageNamed:#"back7.jpg"], [UIImage imageNamed:#"back8.jpg"],[UIImage imageNamed:#"back9.jpg"],[UIImage imageNamed:#"back10.jpg"],[UIImage imageNamed:#"back11.jpg"],[UIImage imageNamed:#"back12.jpg"],[UIImage imageNamed:#"back13.jpg"], nil];
pageControl.numberOfPages=images.count;
for (int i = 0; i < images.count; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
subview = [[UIImageView alloc] initWithFrame:frame];
subview.contentMode = UIViewContentModeScaleAspectFit;
subview.image = [images objectAtIndex:i]; //problima sthn eikona
[self.scrollView addSubview:subview];
}
pageControlBeingUsed = NO;
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * images.count, self.scrollView.frame.size.height);}

fading slideshow in an imageview. xCode 4.2

I am trying to make a slideshow in an imageview that uses also fade-in fade-out effects.
so far ive done this:
- (void)viewDidLoad
{
[super viewDidLoad];
animationView = [[UIImageView alloc] initWithFrame:self.view.frame];
animationView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"photo1.jpg"],
[UIImage imageNamed:#"photo2.jpg"],
[UIImage imageNamed:#"photo3.jpg"], nil];
animationView.animationDuration = 5;
animationView.animationRepeatCount = 0;
[animationView startAnimating];
[self.view addSubview:animationView];
}
images do appear one after the other, in a 5 sec delay, what i want now is to make them fade-in and fade-out each time an image appears, any suggestions on that?
Thanks in advance for your help
I was trying to do something similar. What I did was setup a timer that called a UIView transitionWithView on a UIImageView that was incrementing through the array of my slideshow photos.
- (void)viewDidLoad{
[super viewDidLoad];
self.welcomePhotos = [NSArray arrayWithObjects:#"welcome_1.png",#"welcome_2.png", #"welcome_3.png", nil];
self.imageView1.image = [UIImage imageNamed:[self.welcomePhotos objectAtIndex:0]];
[NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:#selector(transitionPhotos) userInfo:nil repeats:YES];
}
-(void)transitionPhotos{
if (photoCount < [self.welcomePhotos count] - 1){
photoCount ++;
}else{
photoCount = 0;
}
[UIView transitionWithView:self.imageView1
duration:2.0
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{ self.imageView1.image = [UIImage imageNamed:[self.welcomePhotos objectAtIndex:photoCount]]; }
completion:NULL];
}

How can we put two line in UIBarButtonItem in Navigation Bar

"Now Playing" is in One line in UIBarButtonItem. I have to put it in two lines, like "Now" is ay top and "Playing" is at bottom.I have written the following line of code:-
UIBarButtonItem *flipButton = [[UIBarButtonItem alloc]
initWithTitle:#"Now Playing"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(flipView)];
self.navigationItem.rightBarButtonItem = flipButton;
So i want to pu line break in between "Now Playing". So please help me out.
Yes you can. It is fairly simple to do. Create a multiline button and use that. The "trick" is to set the titleLabel numberOfLines property so that it likes multilines.
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
button.titleLabel.numberOfLines = 0;
[button setTitle:NSLocalizedString(#"Now\nPlaying", nil) forState:UIControlStateNormal];
[button sizeToFit];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
When you specify a button type of custom it expects you to configure everything... selected state, etc. which is not shown here to keep the answer focused to the problem at hand.
- (void)createCustomRightBarButton_
{
UILabel * addCustomLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 64, 25)] autorelease];
addCustomLabel.text =#"Now\nPlaying";
addCustomLabel.textColor = [UIColor whiteColor];
addCustomLabel.font = [UIFont boldSystemFontOfSize:11];
addCustomLabel.numberOfLines = 2;
addCustomLabel.backgroundColor = [UIColor clearColor];
addCustomLabel.textAlignment = UITextAlignmentCenter;
CGSize size = addCustomLabel.bounds.size;
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
[addCustomLabel.layer renderInContext: context];
CGImageRef imageRef = CGBitmapContextCreateImage(context);
UIImage * img = [UIImage imageWithCGImage: imageRef];
CGImageRelease(imageRef);
CGContextRelease(context);
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithImage:img
style:UIBarButtonItemStyleBordered
target:self
action:#selector(flipView)]
autorelease];
}
You can host a UIButton as customView inside your bar button (either set the bar button item customView or you can drag one directly on top of your UIBarButtonItem), hook it up as an outlet, then do;
-(void) viewDidLoad
{
//...
self.customButton.titleLabel.numberOfLines = 2;
self.customButton.suggestionsButton.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
self.customButton.suggestionsButton.titleLabel.textAlignment = UITextAlignmentCenter;
}
which in my case becomes
I create 2 PNG images by myself, and it looks good.
UIImage *img = [UIImage imageNamed:#"nowplaying.png"];
UIBarButtonItem *nowPlayingButtonItem = [[[UIBarButtonItem alloc] initWithImage:img style:UIBarButtonItemStyleBordered target:delegate action:#selector(presentNowPlayingMovie)] autorelease];

Program doesn't jump in Delegate-Method

Good Morning Community,
I have a problem, because I have implemented an CoverFlowViewController.m with an Delegate method, but the Debugger NEVER jump in this method, does anyone know why?
Here is the code:
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *weiter = [UIButton buttonWithType:UIButtonTypeRoundedRect];
weiter.frame = CGRectMake(100, 400, 120, 40);
[weiter addTarget:self action:#selector(goToChart) forControlEvents:UIControlEventTouchUpInside];
NSString *ansicht = #"Weiter";
[weiter setTitle:ansicht forState:UIControlStateNormal];
[self.view addSubview:weiter];
// loading images into the queue
loadImagesOperationQueue = [[NSOperationQueue alloc] init];
NSString *imageName;
for (int i=0; i < 10; i++) {
imageName = [[NSString alloc] initWithFormat:#"cover_%d.jpg", i];
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
UIImage *aktuellesImage = imageView.image;
UIImage *scaledImage = [aktuellesImage scaleToSize:CGSizeMake(100.0f, 100.0f)];
[(AFOpenFlowView *)self.view setImage:scaledImage forIndex:i];
}
[(AFOpenFlowView *)self.view setNumberOfImages:10];
}
- (void)openFlowView:(AFOpenFlowView *)openFlowView selectionDidChange:(int)index{
NSLog(#"%d is selected",index);
}
Does anyone know why the Debugger (= the Programm) never jump into the selectionDidChange() method?
I have no solution since three days, and hope somebody could help me?
Greetings and Thank you beforehand
Marco
are you sure you connected the viewDelegate to your viewController?
Please add NSLog(#"Delegate: %#", ((AFOpenFlowView*)self.view).viewDelegate); at the end of your viewDidLoad method to see if it is connected.
Add
((AFOpenFlowView *)self.view).viewDelegate = self; on the init or viewDidLoad method

Resources