现在的位置: 首页 > 综合 > 正文

iPhone开发之UIImageView左右划动切换图片

2012年08月07日 ⁄ 综合 ⁄ 共 3143字 ⁄ 字号 评论关闭

上下划动可以用类似的方法实现:

#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
【上篇】
【下篇】

抱歉!评论已关闭.