iPhone開發之UIImageView左右划動切換圖片

來源:互聯網
上載者:User

上下划動可以用類似的方法實現:

#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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.