根据 UIPanGestureRecognizer 翻译值更改 UIImageView 中的图像
Posted
技术标签:
【中文标题】根据 UIPanGestureRecognizer 翻译值更改 UIImageView 中的图像【英文标题】:Change image in UIImageView based on UIPanGestureRecognizer translation value 【发布时间】:2012-12-14 15:02:34 【问题描述】:我在数组中有 8 张图像,UIImageView
一次显示数组中的一张图像。我在UIImageView
上也有UIPanGestureRecognizer
,所以当用户在图像上横向移动手指时,它会改变(有点像图像顶部的隐形滚动条)。我正在使用平移手势的平移值来更改图像。这一切都有效,但不是我想要的方式。图像变化太快,只需短手指平移即可滚动图像。有没有办法需要更长的平移来更改图像?或者任何其他方法可以让用户“更顺畅”?
.h
@interface FirstViewController : UIViewController
@property (nonatomic, retain) IBOutlet UIImageView *imageView;
- (IBAction)pan:(UIGestureRecognizer *)panGesture;
.m
@interface FirstViewController ()
NSArray * imageArray;
int _currentIndex;
@end
@implementation FirstViewController
- (IBAction)pan:(UIPanGestureRecognizer *)panGesture
// NSLog(@"panned");
CGPoint translation = [panGesture translationInView:self.view];
[self.panLabel setText: NSStringFromCGPoint(translation)];
NSLog(@"%@ translation", NSStringFromCGPoint(translation));
if (translation.x<-1)
_currentIndex=(_currentIndex<=0)?0:_currentIndex-1;
UIImage *img = [UIImage imageNamed:[imageArray objectAtIndex:_currentIndex]];
[self.imageView setImage:img];
else if(translation.x>1)
_currentIndex=(_currentIndex>=7)?7:_currentIndex+1;
UIImage *img = [UIImage imageNamed:[imageArray objectAtIndex:_currentIndex]];
[self.imageView setImage:img];
[panGesture setTranslation:CGPointZero inView:self.view];
- (void)viewDidLoad
[super viewDidLoad];
imageArray = [[NSArray alloc]
initWithObjects:@"image1.jpg", @"image2.jpg", @"image3.jpg", @"image4.jpg", @"image5.jpg", @"image6.jpg", @"image7.jpg", @"image8.jpg", nil];
【问题讨论】:
【参考方案1】:您可以修改代码以跟踪 X 平移随着时间的推移。这将允许您为您的锅量设置一个变量。修改后的代码如下:
@interface FirstViewController ()
NSArray * imageArray;
int _currentIndex;
CGFloat _currentPanXAmount;
CGFloat _panThreshold;
@end
@implementation FirstViewController
- (IBAction)pan:(UIPanGestureRecognizer *)panGesture
CGPoint translation = [panGesture translationInView:self.view];
_currentPanXAmount += translation.x;
[self.panLabel setText: NSStringFromCGPoint(translation)];
NSLog(@"%@ translation", NSStringFromCGPoint(translation));
if (_currentPanXAmount < (-1 * _panThreshold))
_currentIndex=(_currentIndex<=0)?0:_currentIndex-1;
UIImage *img = [UIImage imageNamed:[imageArray objectAtIndex:_currentIndex]];
[self.imageView setImage:img];
_currentPanXAmount = 0.0;
else if(_currentPanXAmount > _panThreshold)
_currentIndex=(_currentIndex>=7)?7:_currentIndex+1;
UIImage *img = [UIImage imageNamed:[imageArray objectAtIndex:_currentIndex]];
[self.imageView setImage:img];
_currentPanXAmount = 0.0;
[panGesture setTranslation:CGPointZero inView:self.view];
- (void)viewDidLoad
[super viewDidLoad];
imageArray = [[NSArray alloc]
initWithObjects:@"image1.jpg", @"image2.jpg", @"image3.jpg", @"image4.jpg", @"image5.jpg", @"image6.jpg", @"image7.jpg", @"image8.jpg", nil];
// Make this larger for slower transitions and smaller for faster transitions.
_panThreshold = 10.0;
【讨论】:
这正是我想要实现的。非常感谢! 关于图像前进的方向:从右到左平移时,您从 8 ---> 1 开始。必须颠倒“if 语句”才能有更直观的“滚动”。你还缺少一个括号,现在我正在编辑它。 @robhasacamera _currentIndex=(_currentIndex>=7)?7:_currentIndex+1;你怎么翻译这是swift2?以上是关于根据 UIPanGestureRecognizer 翻译值更改 UIImageView 中的图像的主要内容,如果未能解决你的问题,请参考以下文章
使用 UIPanGestureRecognizer 拖动 + 旋转触摸下车
使用 UIPanGestureRecognizer- Swift 3 旋转 ImageView
使用 UIPanGestureRecognizer UINavigationController 向后滑动不起作用