ios - 根据 UIPanGestureRecognizer 翻译值更改 UIImageView 中的图像

标签 ios xcode uipangesturerecognizer

我在数组中有 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];
}

最佳答案

您可以修改代码以随时间跟踪 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;
}

关于ios - 根据 UIPanGestureRecognizer 翻译值更改 UIImageView 中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13881144/

相关文章:

ios - App Store:iOS9有效,iOS8提供 “Unable to download application. <App> could not be installed at this time”

xcode - TableView 与 Xib 文件的协议(protocol) UITableViewDataSource 的冗余一致性

ios - 如何使用手势

ios - 更改以相同手势将一个 View 拖动到另一个 View

swift - 在 Swift 中移动 UIImageView 的 UIImage 中的 UIView

iOS/Swift - 有没有办法将我们的应用程序链接为 WhatsApp 消息?

ios - Xcode - 如何修复 'NSUnknownKeyException',原因 : … this class is not key value coding-compliant for the key X"error?

ios - 当我从 URL 获取文本时,如何删除那些行?

iOS 应用程序 'The application could not be verified'

ios - Alamofire 5 在使用 Carthage 安装后显示错误, "' Alamofire 没有名为请求的成员”