Ios 基于UIPangestureRecognitor转换值更改UIImageView中的图像
我在阵列中有8个图像,Ios 基于UIPangestureRecognitor转换值更改UIImageView中的图像,ios,xcode,uipangesturerecognizer,Ios,Xcode,Uipangesturerecognizer,我在阵列中有8个图像,UIImageView一次显示阵列中的一个图像。我还有UIImageView上的uipangestureerecognizer,因此当用户在图像上横向移动手指时,手指会发生变化(类似于图像顶部的不可见滚动条)。我正在使用平移手势的平移值来更改图像。这一切都是可行的,但不是我想要的方式。图像变化太快,只需短手指在图像中滚动。是否有一种方法需要较长的平移时间来更改图像?或者其他任何让用户感觉更“流畅”的方法 h m @interface FirstViewController(
UIImageView
一次显示阵列中的一个图像。我还有UIImageView
上的uipangestureerecognizer
,因此当用户在图像上横向移动手指时,手指会发生变化(类似于图像顶部的不可见滚动条)。我正在使用平移手势的平移值来更改图像。这一切都是可行的,但不是我想要的方式。图像变化太快,只需短手指在图像中滚动。是否有一种方法需要较长的平移时间来更改图像?或者其他任何让用户感觉更“流畅”的方法
h
m
@interface FirstViewController(){
NSArray*图像阵列;
int currentIndex;
}
@结束
@FirstViewController的实现
-(iAction)pan:(UIPangEstureRecognitor*)panGesture{
//NSLog(@“平移”);
CGPoint translation=[panGesture translationView:self.view];
[self.panLabel setText:NSStringFromCGPoint(翻译)];
NSLog(@“%@翻译”,NSStringFromCGPoint(翻译));
如果(translation.x=7)?7:_currentIndex+1;
UIImage*img=[UIImage ImageName:[imageArray objectAtIndex:_currentIndex];
[self.imageView setImage:img];
}
[panGesture setTranslation:CGPointZero-inView:self.view];
}
-(无效)viewDidLoad{
[超级视图下载];
imageArray=[[NSArray alloc]
initWithObjects:@“image1.jpg”@“image2.jpg”@“image3.jpg”@“image4.jpg”@“image5.jpg”@“image6.jpg”@“image7.jpg”@“image8.jpg”,nil];
}
您可以修改代码以随时间跟踪X平移。这将允许您为pan金额设置一个变量。修改后的代码如下:
@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;
}
@interface FirstViewController(){
NSArray*图像阵列;
int currentIndex;
CGFloat _currentPanXAmount;
CGFloat_panThreshold;
}
@结束
@FirstViewController的实现
-(iAction)pan:(UIPangEstureRecognitor*)panGesture{
CGPoint translation=[panGesture translationView:self.view];
_currentPanXAmount+=translation.x;
[self.panLabel setText:NSStringFromCGPoint(翻译)];
NSLog(@“%@翻译”,NSStringFromCGPoint(翻译));
如果(_currentPanXAmount<(-1*_panThreshold)){
_currentIndex=(\u currentIndex\u panThreshold){
_currentIndex=(\u currentIndex>=7)?7:\u currentIndex+1;
UIImage*img=[UIImage ImageName:[imageArray objectAtIndex:_currentIndex];
[self.imageView setImage:img];
_currentPanXAmount=0.0;
}
[panGesture setTranslation:CGPointZero-inView:self.view];
}
-(无效)viewDidLoad{
[超级视图下载];
imageArray=[[NSArray alloc]
initWithObjects:@“image1.jpg”@“image2.jpg”@“image3.jpg”@“image4.jpg”@“image5.jpg”@“image6.jpg”@“image7.jpg”@“image8.jpg”,nil];
//对于较慢的过渡,将其调大,对于较快的过渡,将其调小。
_panThreshold=10.0;
}
您可以修改代码以跟踪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;
}
@interface FirstViewController(){
NSArray*图像阵列;
int currentIndex;
CGFloat _currentPanXAmount;
CGFloat_panThreshold;
}
@结束
@FirstViewController的实现
-(iAction)pan:(UIPangEstureRecognitor*)panGesture{
CGPoint translation=[panGesture translationView:self.view];
_currentPanXAmount+=translation.x;
[self.panLabel setText:NSStringFromCGPoint(翻译)];
NSLog(@“%@翻译”,NSStringFromCGPoint(翻译));
如果(_currentPanXAmount<(-1*_panThreshold)){
_currentIndex=(\u currentIndex\u panThreshold){
_currentIndex=(\u currentIndex>=7)?7:\u currentIndex+1;
UIImage*img=[UIImage ImageName:[imageArray objectAtIndex:_currentIndex];
[self.imageView setImage:img];
_currentPanXAmount=0.0;
}
[panGesture setTranslation:CGPointZero-inView:self.view];
}
-(无效)viewDidLoad{
[超级视图下载];
imageArray=[[NSArray alloc]
initWithObjects:@“image1.jpg”@“image2.jpg”@“image3.jpg”@“image4.jpg”@“image5.jpg”@“image6.jpg”@“image7.jpg”@“image8.jpg”,nil];
//对于较慢的过渡,将其调大,对于较快的过渡,将其调小。
_panThreshold=10.0;
}
这正是我想要实现的目标。非常感谢!关于图像的前进方向:从右向左平移时,从8--->1。必须反转“if语句”以获得更直观的“滚动”。您还缺少一个括号,我正在编辑该括号。@robhasacamera currentIndex=(currentIndex>=7)?7:currentIndex+1;如何翻译这是swift2?这正是我试图实现的。非常感谢!关于图像的前进方向:从右向左平移时,从8--->1“如果语句”必须颠倒,才能更直观地“滚动”。此外,您还缺少一个括号,我现在正在编辑该括号。@robhasacamera currentIndex=(currentIndex>=7)?7:currentIndex+1;如何翻译这是swift2?
@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;
}