ビューが必要な私の .m クラスと、そのビューに imageViewSlider(uiimageview) が追加されます。アクティビティ インジケーターと分離されたイメージのフェッチと組み合わせて、動作するかどうかを確認します。これはスクロールビューなしでスライドを実装します
#import "ImageSliderView.h"
#import <QuartzCore/QuartzCore.h>
#define kAnimationKey @"animationKey"
@interface ImageSliderView ()
@end
@implementation ImageSliderView
@synthesize imageData;
@synthesize imageViewSlider;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Set GestureRecoginzer..
//******************************************************Swipe recognisers**************************
UISwipeGestureRecognizer *rightRecognizer=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeHandle:)];
rightRecognizer.direction=UISwipeGestureRecognizerDirectionRight;
[rightRecognizer setNumberOfTouchesRequired:1];
[self.view addGestureRecognizer:rightRecognizer];
[rightRecognizer release];
UISwipeGestureRecognizer *leftRecognizer=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeHandle:)];
leftRecognizer.direction=UISwipeGestureRecognizerDirectionLeft;
[leftRecognizer setNumberOfTouchesRequired:1];
[self.view addGestureRecognizer:leftRecognizer];
[leftRecognizer release];
//******************************************************
currentPage=0;
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[self setImageData:nil];
[self setImageViewSlider:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
-(void)dealloc
{
[imageData release];
[imageViewSlider release];
[super dealloc];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark ---swipe
-(void)swipeHandle:(UISwipeGestureRecognizer *)guestureRecognizer
{
if(guestureRecognizer.direction==UISwipeGestureRecognizerDirectionLeft)
{
[self nextButtonAndSlideForwad];
}
if(guestureRecognizer.direction==UISwipeGestureRecognizerDirectionRight)
{
[self prevButtonAndSlideBack];
}
}
-(void)nextButtonAndSlideForwad
{
self.imageViewSlider.backgroundColor=[UIColor redColor];
[self slideForward];
}
-(void)prevButtonAndSlideBack
{
self.imageViewSlider.backgroundColor=[UIColor greenColor];
[self slideBackward];
}
-(void)slideForward
{
CATransition *animation = [CATransition animation];
[animation setDelegate:self];
[animation setType:kCATransitionPush];
[animation setSubtype:kCATransitionFromRight];
[animation setDuration:0.4f];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
[[self.view layer] addAnimation:animation forKey:kAnimationKey];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.55];
[UIView commitAnimations];
}
-(void)slideBackward
{
CATransition *animation = [CATransition animation];
[animation setDelegate:self];
[animation setType:kCATransitionPush];
[animation setSubtype:kCATransitionFromLeft];
[animation setDuration:0.4f];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[self.view exchangeSubviewAtIndex:1 withSubviewAtIndex:0];
[[self.view layer] addAnimation:animation forKey:kAnimationKey];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.55];
[UIView commitAnimations];
}
@end