转:iPhone开辟之UIImageView阁下…

本文介绍了一种通过触摸滑动实现图片轮播效果的方法,包括初始化视图控制器、配置图片数组、处理触摸事件以及动画效果的实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

高低划动可以用类似的办法实现:



#import 


@interface Abstruct : UIViewController {
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:@or(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:@or(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:@or(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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值