【发布时间】:2014-07-31 03:40:38
【问题描述】:
我无法显示图片,因为我没有足够的声誉,但我试图在视图中有一个图像视图,其中包含一系列图片,您可以滑动以从一个图像过渡到下一个图像。但唯一改变的是图像视图中的图像,而不是整个视图。
我认为它类似于堆栈溢出的这个问题 here
我理解手势部分,它可以工作,但它不像我想要的那样动画。我希望它像您在照片应用程序上滑动照片时一样工作。有什么帮助吗?
这是我的代码,但我想我只需要将图像视图设为子视图
#import "PizzaViewController.h"
@interface PizzaViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *myImageView;
@property (nonatomic, strong) NSMutableArray *imgArray;
@end
@implementation PizzaViewController
@synthesize imgArray;
- (void)viewDidLoad
{
[super viewDidLoad];
imgArray = [[NSMutableArray alloc] initWithObjects:@"recipe instructions 1-01.png",@"recipe instructions 2-01.png",@"recipe instructions 3-01.png", nil];
indexToShow = 0;
UISwipeGestureRecognizer *gestureRight;
UISwipeGestureRecognizer *gestureLeft;
gestureRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRight:)];
gestureLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeft:)];
[gestureLeft setDirection:(UISwipeGestureRecognizerDirectionLeft)];
[[self view] addGestureRecognizer:gestureRight];
[[self view] addGestureRecognizer:gestureLeft];
self.myImageView.image = [UIImage imageNamed:[imgArray objectAtIndex:indexToShow]];
}
- (void)swipeRight:(UISwipeGestureRecognizer *)gesture
{
if ((gesture.state == UIGestureRecognizerStateChanged) ||
(gesture.state == UIGestureRecognizerStateEnded)) {
if ((indexToShow-1) < -1) {
indexToShow = imgArray.count-1;
}
self.myImageView.image = [UIImage imageNamed:[imgArray objectAtIndex:indexToShow]];
indexToShow--;
}
}
- (void)swipeLeft:(UISwipeGestureRecognizer *)gesture
{
if ((gesture.state == UIGestureRecognizerStateChanged) ||
(gesture.state == UIGestureRecognizerStateEnded)) {
if ((indexToShow+1) > imgArray.count ) {
indexToShow = 0;
}
self.myImageView.image = [UIImage imageNamed:[imgArray objectAtIndex:indexToShow]];
indexToShow++;
}
}
@结束
【问题讨论】:
-
你必须使用scrollView并将你所有的imageview放在scrollView里面。
-
您可以在视图或图像视图上使用 SwipeGestureRecognizer。我在我的一个项目中做了同样的事情,而且效果很好。
-
我的代码对你有用吗?
-
没有。抱歉,我问的不是这个。
-
“我理解手势部分,它可以工作,但它的动画效果不像我想要的那样。我希望它像你在照片应用上滑动照片时一样工作”。我做了更多的研究,我想我可能不得不使用 panGesture 或滚动视图
标签: ios uiimageview uigesturerecognizer subview