Cocoa touch 到UIImageView的动画
我创建了一个视图控制器,并在其中放置了一个UIImageView,我制作了一个包含图像的Cocoa touch 到UIImageView的动画,cocoa-touch,uiimage,Cocoa Touch,Uiimage,我创建了一个视图控制器,并在其中放置了一个UIImageView,我制作了一个包含图像的NSArray,我传入了UIImageView,我添加了一个UIWipegestureRecognitor,用于左扫和右扫 这段代码可以工作,但图像是在没有效果或过渡的情况下像隆隆一样滑动时出现的,有没有办法在它们之间添加一个简单的过渡 代码如下: NSArray *images = [[NSArray alloc] initWithObjects: @"1-r.png",
NSArray
,我传入了UIImageView
,我添加了一个UIWipegestureRecognitor
,用于左扫和右扫
这段代码可以工作,但图像是在没有效果或过渡的情况下像隆隆一样滑动时出现的,有没有办法在它们之间添加一个简单的过渡
代码如下:
NSArray *images = [[NSArray alloc] initWithObjects:
@"1-r.png",
@"2-r.png",
@"3-r.png",
@"4-r.png",
@"5-r.png",
nil];
UISwipeGestureRecognizerDirection direction = [(UISwipeGestureRecognizer *)sender direction];
switch (direction) {
case UISwipeGestureRecognizerDirectionLeft:
imageIndex++;
break;
case UISwipeGestureRecognizerDirectionRight:
imageIndex--;
break;
default:
break;
}
imageIndex = (imageIndex < 0) ? ([images count] - 1) : imageIndex % [images count];
_imageView.image = [UIImage imageNamed:[images objectAtIndex:imageIndex]];
NSArray*images=[[NSArray alloc]initWithObjects:
@“1-r.png”,
@“2-r.png”,
@“3-r.png”,
@“4-r.png”,
@“5-r.png”,
零];
UISweepGestureRecognitzerDirection=[(UISweepGestureRecognitzer*)发送方方向];
开关(方向){
案例UISweepGestureRecognitizerDirectionLeft:
imageIndex++;
打破
案例UISweepGestureRecognitizerDirectionRight:
图像索引--;
打破
违约:
打破
}
imageIndex=(imageIndex<0)?([图像计数]-1):图像索引%[图像计数];
_imageView.image=[UIImage ImageName:[images objectAtIndex:imageIndex]];
将UIImgaView添加到viewcontroller并创建iBooted imgView。将以下代码添加到viewcontroller的.m文件中
- (UIImage *) prev {
NSInteger index = [self.imgView.animationImages indexOfObject: self.imgView.image];
if (index > 0) {
return [self.imgView.animationImages objectAtIndex: index - 1];
}
return self.imgView.animationImages.lastObject;
}
- (UIImage *) next {
NSInteger index = [self.imgView.animationImages indexOfObject: self.imgView.image];
if (index < self.imgView.animationImages.count - 1) {
return [self.imgView.animationImages objectAtIndex: index + 1];
}
return [self.imgView.animationImages objectAtIndex: 0];
}
- (void) swipeRight {
UIImageView *topView = [[UIImageView alloc] initWithFrame: self.imgView.frame];
topView.image = [self prev];
topView.alpha = 0.0;
[self.view addSubview: topView];
[self animateImages];
}
- (void) swipeLeft {
UIImageView *topView = [[UIImageView alloc] initWithFrame: self.imgView.frame];
topView.image = [self next];
topView.alpha = 0.0;
[self.view addSubview: topView];
[self animateImages];
}
- (void) animateImages {
UIImageView *topView = self.view.subviews.lastObject;
[UIView animateWithDuration: 0.5 delay: 0.0f options: UIViewAnimationOptionCurveEaseInOut animations:
^(void) {
topView.alpha = 1.0;
} completion: ^(BOOL finished) {
self.imgView.image = topView.image;
[topView removeFromSuperview];
}];
}
- (void) viewDidLoad {
NSArray *imageNames = [[NSArray alloc] initWithObjects:
@"1-r.png",
@"2-r.png",
@"3-r.png",
@"4-r.png",
@"5-r.png",
nil];
NSMutableArray *images = [NSMutableArray array];
for (NSString *filename in imageNames) {
[images addObject: [UIImage imageNamed: filename]];
}
self.imgView.animationImages = images;
self.imgView.userInteractionEnabled = YES;
self.imgView.image = [images objectAtIndex: 0];
UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget: self action: @selector(swipeRight)];
rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;
UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget: self action: @selector(swipeLeft)];
leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft;
[self.imgView addGestureRecognizer: leftSwipe];
[self.imgView addGestureRecognizer: rightSwipe];
}
-(UIImage*)上一个{
NSInteger索引=[self.imgView.animationImages索引对象:self.imgView.image];
如果(索引>0){
返回[self.imgView.animationImages对象索引:索引-1];
}
返回self.imgView.animationImages.lastObject;
}
-(UIImage*)下一步{
NSInteger索引=[self.imgView.animationImages索引对象:self.imgView.image];
如果(索引
这是Cocoa Touch,而不是Cocoa(那将是一个OS X问题)。考虑正确地格式化代码。必要时添加空白会使代码更具可读性。杜德,这段代码不起作用。它给了我这样一句话:“从枚举类型'enum uiviewanimation'隐式转换,我们之前提到的警告已经消失,但NSArray中的照片仍然显示,没有转换plzz帮助。plzz如果,你能为我做一个包含uiimage的示例吗?它包含图像,当向左或向右滑动时,数组中的另一个图像会显示为带有过渡的图像。这是我的邮件,如果你想帮我解决这个问题:alaahershy@gmail.comThanks谢谢你的帮助,但忘了它吧,它会使我的应用程序崩溃,如果你想把这个示例发送给我的邮件以了解发生了什么事,请提前向我表示巨大的感谢