我无法显示图片,因为我没有足够的声誉,但我想在视图中设置一个图像视图,其中包含一系列图片,您可以滑动以从一个图像过渡到下一个图像。但唯一改变的是图像视图中的图像,而不是整个视图。
我认为这与堆栈溢出问题类似 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++;
}
}
@end
答案 0 :(得分:1)
要使其像照片一样移动,您需要将UImageView放入UIScrollView
如果您不希望它像照片一样移动,请执行以下操作:
制作一个包含NSArray
的{{1}}。创建一个int,它将是要显示的数组中图像的索引。这样,手势识别器会更改一个int值(当向左滑动减一时,向右滑动一次)。每次滑动后,将UImage
中的图像更改为int值索引处的对象。
代码示例:
设置数组
UImageView
后退和下一个按钮
- (void) setImages
{
UIImage *image1 = [UIImage imageNamed:@"1.png"];
UIImage *image2 = [UIImage imageNamed:@"2.png"];
UIImage *image3 = [UIImage imageNamed:@"3.png"];
UIImage *image4 = [UIImage imageNamed:@"4.png"];
UIImage *image5 = [UIImage imageNamed:@"5.png"];
UIImage *image6 = [UIImage imageNamed:@"6.png"];
UIImage *image7 = [UIImage imageNamed:@"7.png"];
UIImage *image8 = [UIImage imageNamed:@"8.png"];
NSArray *pictuesArray = [[NSArray alloc] initWithObjects:image1, image2, image3, image4, image5, image6, image7, image8, nil];
self.pictures = pictuesArray;
}