Reputation: 2067
i have done simple animation but want to add animation to images like from left to right like a slider, i have array of images, how to animate programmatically,
-(void) slideImages
{
NSMutableArray *imagesArrays= [[NSMutableArray alloc]init];
for (SliderDC *slider in imagesArray) {
sliderImageName = slider.sliderImagePath;
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString: [NSString stringWithFormat:@"%@%@",KWURL ,sliderImageName]]];
UIImage *imgOne = [UIImage imageWithData:imageData];
if(imgOne)
[imagesArrays addObject:imgOne];
}
// after adding all image in array then add animation.
UIImageView* animatedImageView;
if ([[UIScreen mainScreen] bounds].size.height == 568){
animatedImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 65, 320, 150)];
}
else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
animatedImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 98, 768, 228)];
}
else
{
animatedImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 65, 320, 118)];
}
animatedImageView.animationImages = imagesArrays;
animatedImageView.animationDuration = 20.0f;
animatedImageView.animationRepeatCount = 0;
[self.view addSubview: animatedImageView];
[animatedImageView startAnimating];
}
Upvotes: 0
Views: 3630
Reputation: 2451
try this..
-(void) slideImages
{
NSMutableArray *imagesArrays= [[NSMutableArray alloc]init];
for (SliderDC *slider in imagesArray) {
sliderImageName = slider.sliderImagePath;
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString: [NSString stringWithFormat:@"%@%@",KWURL ,sliderImageName]]];
UIImage *imgOne = [UIImage imageWithData:imageData];
if(imgOne)
[imagesArrays addObject:imgOne];
}
- (IBAction)startAnimation:(UIButton *)sender
{
slideTransition = [CATransition animation]; // CATransition * slideTransition; instance variable
slideTransition.duration = 0.35;
slideTransition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
slideTransition.type = kCATransitionPush;
slideTransition.delegate = self;
NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:0.50 target:self selector:@selector(slideShow) userInfo:nil repeats:YES];
[timer fire];
}
-(void)slideShow
{
slideTransition.subtype =kCATransitionFromLeft; // or kCATransitionFromRight
[animatedImageView.layer addAnimation:slideTransition forKey:nil];
if (index < imagesArrays.count-1) // NSUInteger index; instance variable
{
index++;
}
else
{
index=0;
}
animatedImageView.image =[imagesArrays objectAtIndex:index];
}
Upvotes: 2
Reputation: 5754
-(void) slideImages
{
NSMutableArray *imagesArrays= [[NSMutableArray alloc]init];
for (SliderDC *slider in imagesArray) {
sliderImageName = slider.sliderImagePath;
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString: [NSString stringWithFormat:@"%@%@",KWURL ,sliderImageName]]];
UIImage *imgOne = [UIImage imageWithData:imageData];
if(imgOne)
[imagesArrays addObject:imgOne];
}
UIImageView* animatedImageView;
if ([[UIScreen mainScreen] bounds].size.height == 568){
animatedImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 65, 320, 150)];
}
else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
animatedImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 98, 768, 228)];
}
else
{
animatedImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 65, 320, 118)];
}
[self.view addSubview: animatedImageView];
self.animatedImageView.animationImages = imagesArrays;
self.animatedImageView.animationDuration=1.0;
self.animatedImageView.animationRepeatCount=1;
self.animatedImageView.image = [self.imageViewAnimation.animationImages lastObject]; //to stop animation at last image.
[self.animatedImageView startAnimating];
Upvotes: 1
Reputation: 6524
You can simply use DRDynamicSlideShow
Create beautiful, animated, paging UIScrollViews. Easily animate views as the UIScrollView gets paged. This is perfect for welcome screens and introduction views in iOS apps.
Features
1 line of code per animation.
Any type of value can be animated.
Block-driven.
Upvotes: 2