fading slideshow in an imageview. xCode 4.2 - xcode

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

Related

CABasicAnimation not working with UIImageView

I have a UIImageView that is called upon programatically, I am trying to get it to spin around but its not working. The image will be placed inside a dynamic UITableView (I cannot change it to static). The image appears fine in my table view but it just doesn't spin.
- (void)viewDidLoad
{
[super viewDidLoad];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 122, 38, 38)];
imageView.image = [UIImage imageNamed:#"settings1.png"];
[self.view addSubview:imageView];
CABasicAnimation *spin;
spin = [CABasicAnimation animationWithKeyPath:#"transform.rotation"];
spin.fromValue = [NSNumber numberWithFloat:0];
spin.toValue = [NSNumber numberWithFloat:((360*M_PI)/180)];
spin.duration = 4;
spin.repeatCount = 10*1000;
[imageView.layer addAnimation:spin forKey:#"360"];
It's probably because you're trying to start the animation in viewDidLoad. I'd suggest reorganizing your code in such a way that you create the image view instance in viewDidLoad, but then wait until viewDidAppear: is called to actually start the animation.
#property (strong, nonatomic) UIImageView *imageView;
And then
- (void)viewDidLoad
{
[super viewDidLoad];
self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 122, 38, 38)];
self.imageView.image = [UIImage imageNamed:#"settings1.png"];
[self.view addSubview:self.imageView];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
CABasicAnimation *spin = [CABasicAnimation animationWithKeyPath:#"transform.rotation"];
spin.fromValue = [NSNumber numberWithFloat:0];
spin.toValue = [NSNumber numberWithFloat:((360*M_PI)/180)];
spin.duration = 4;
spin.repeatCount = 10*1000;
[self.imageView.layer addAnimation:spin forKey:#"360"];
}

Animate image from array with animation style

I have 3 images in my array for this sample code it just animate without any style .I want to animate like fade in fade out style.
NSArray *imarray = [[NSArray alloc] initWithObjects:
[UIImage imageWithContentsOfFile:[NSString stringWithFormat:#"%#/lady1_open.png", path]],
[UIImage imageWithContentsOfFile:[NSString stringWithFormat:#"%#/lady1_open2.png", path]],
[UIImage imageWithContentsOfFile:[NSString stringWithFormat:#"%#/lady1_open3.png", path]],
nil];
bingo_girl.animationImages=imarray;
bingo_girl.animationDuration=5;
bingo_girl.animationRepeatCount=0;
[bingo_girl.layer addAnimation:transition forKey:nil];
[bingo_girl startAnimating];
You may animate images with transition manually for example using this category
#implementation UIImageView (AnimateTransition)
- (void) assignImage:(UIImage *)image withTransition:(NSString *)withTransition withDirection:(NSString *)withDirection{
if(image == nil)
{
[self setImage:nil];
return;
}
if ([UIImagePNGRepresentation(self.image) isEqualToData:UIImagePNGRepresentation(image)]) {
return;
}
CATransition *animation = [CATransition animation];
animation.duration = 0.2;
animation.type = withDirection;
animation.subtype = withDirection;
[[self layer] addAnimation:animation forKey:#"imageFade"];
[self setImage:image];
}
#end

Simple Animation in Cocoa Objective-C

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.

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

how to create horizontal spinner+timer like this?

is it possible to create a design something like this?
any help regarding this would be very helpful for me.the functionality i want is to rotate the wheel left-to-right or right to left for selecting time...yellow is selection color and red is shows the remaining time when countdown is running
Use a Gallery view object. Just fill it with the images you want to scroll horizontally.
This would be a good approximation to what you're looking to achieve.
See my video
http://www.youtube.com/watch?v=4acFshAlGJ8
I have use
https://lh3.googleusercontent.com/-WZEDMSmfrK0/TeeD93t8qYI/AAAAAAAAAKw/X9D6jRkLfLk/s800/MUKScale.png
image as Scale in the scrollView this is an incomplete example just to show how can it is achievable.
here is some helpful piece of code,
in this approx everything is static but for the real work one should to work more,
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:scrollView];
UIImageView *backImgg = [[UIImageView alloc] initWithFrame:CGRectMake(x,y,886,15)];
[backImgg setImage: [UIImage imageNamed:#"MUKScale.png"]];//Image in the link above
[scrollView addSubview:backImgg];
[backImgg release];
[scrollView setContentSize:CGSizeMake(886,15)];
return;
}
NSTimer *timer ;
float timeSet =0 ;
-(IBAction) btnPressed
{
timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:#selector(showTimeInLbl) userInfo:nil repeats:YES];
}
-(void)showTimeInLbl
{
CGRect visibleRect;
visibleRect.origin = scrollView.contentOffset;
visibleRect.size = scrollView.contentSize;
NSLog( #"Visible rect: %#", NSStringFromCGRect(visibleRect) );
float time = visibleRect.origin.x / 8;
timeSet = time;
lblTime.text = [NSString stringWithFormat:#"%f",time];
[UIView animateWithDuration: .1
animations: ^{
[scrollView setContentOffset:CGPointMake(visibleRect.origin.x - 8,0) animated:NO];
}completion: ^(BOOL finished){
}
];
timeSet-=0.1;
lblTime.text = [NSString stringWithFormat:#"%f",timeSet];
if(timeSet<=0)
{
[timer invalidate];
lblTime.text = #"0";
[UIView animateWithDuration: .1
animations: ^{
[scrollView setContentOffset:CGPointMake(0,0) animated:NO];
}completion: ^(BOOL finished){
}
];
}
}
You can use a Never Ended scrollView (For example this you have in it like off paging).
Now by this you will achieve the scale and by using page no or coordinates of the scrollview you can find the scale reading to show on above.
On start count down create animation of UIView contentOffset CGPointMake(10, 100).
for example:
[UIView animateWithDuration: 1.5 /* Give Duration which was also on top */
animations: ^{
[scrollView setContentOffset:CGPointMake(0,0) animated:NO];
}completion: ^(BOOL finished){ }];

Resources