上下划動可以用類似的方法實現:
#import <UIKit/UIKit.h>@interface Abstruct : UIViewController <UIScrollViewDelegate>{ UIImageView* myimageview; NSMutableArray *imgArray; CGPoint FirstPoint; CGPoint SecondPoint; NSInteger Page; BOOL touchMove;}@property (retain, nonatomic) UIImageView* myimageview;@property (retain, nonatomic) NSMutableArray *imgArray;@property NSInteger Page;@end
- (void)viewDidLoad{ [super viewDidLoad];imgArray = [[NSMutableArray alloc] initWithObjects: [UIImage imageNamed:@"01-01-1.jpg"], [UIImage imageNamed:@"01-01-2.jpg"], [UIImage imageNamed:@"01-01-3.jpg"], [UIImage imageNamed:@"01-01-4.jpg"], [UIImage imageNamed:@"01-01-5.jpg"], [UIImage imageNamed:@"01-01-6.jpg"], [UIImage imageNamed:@"01-01-7.jpg"],nil]; self.navigationItem.title = [NSString stringWithFormat:@"1/%d",[imgArray count]]; self.myimageview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 44,768, 1004)]; [self.myimageview setImage:[imgArray objectAtIndex:Page]]; [self.view addSubview:myimageview];}/*======================================================= //觸摸事件:點擊彈出導航條,左右划動切換經文 ========================================================*/- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [[event allTouches] anyObject]; CGPoint point = [touch locationInView:self.view]; FirstPoint = point; touchMove = NO;}- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint point = [touch locationInView:self.view]; SecondPoint = point; touchMove = YES;}- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ if(touchMove == NO) { }else { if (FirstPoint.x > SecondPoint.x) { if(Page < [imgArray count] - 1) { Page++; CGContextRef context = UIGraphicsGetCurrentContext(); [UIView beginAnimations:nil context:context]; [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; [myimageview setAlpha:0.0]; [UIView setAnimationDuration:0.05]; [UIView commitAnimations]; [self performSelector:@selector(ChangeImage) withObject:nil afterDelay:0.1]; }else if (Page == [imgArray count] -1) { CGContextRef context = UIGraphicsGetCurrentContext(); [UIView beginAnimations:nil context:context]; [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; [myimageview setAlpha:0.0]; [UIView setAnimationDuration:0.05]; [UIView commitAnimations]; Page = 0; [self performSelector:@selector(ChangeImage) withObject:nil afterDelay:0.1]; } }else { if(Page >= 1) { Page--; CGContextRef context = UIGraphicsGetCurrentContext(); [UIView beginAnimations:nil context:context]; [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; [myimageview setAlpha:0.0]; [UIView setAnimationDuration:0.05]; [UIView commitAnimations]; [self performSelector:@selector(ChangeImage) withObject:nil afterDelay:0.1]; } } } }/*======================================================= 更新圖片顯示 ========================================================*/-(void)ChangeImage{ CGContextRef context = UIGraphicsGetCurrentContext(); [UIView beginAnimations:nil context:context]; [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; [UIView setAnimationDuration:0.5]; //動畫長度,單位為秒 [self.view setAlpha:1]; [myimageview setAlpha:1]; [myimageview setImage:[imgArray objectAtIndex:Page]]; //[myimageview setAnimationRepeatCount:4]; //設定重複次數 [UIView commitAnimations]; self.navigationItem.title = [NSString stringWithFormat:@"%d/%d",Page+1,[imgArray count]];}@end