使用UITouch方法拖动UIImageView图像

时间:2015-02-25 15:41:50

标签: ios objective-c uiimageview

我有一个嵌入了UIImageViews的UITableViewCell。我理解这不常见,但我使用UITouch方法完成水平滑动。我从url / api中检索图像。问题是我希望从左到右一次拖动一个图像。问题是,当我在imageView上拖动手指时,所有加载的图像都会快速滑过。我希望在拖动过程中一次关注一个图像。希望我很清楚。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
  startLocation = [[touches anyObject] locationInView:self];
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
  CGPoint point = [[touches anyObject] locationInView:self];
  UITouch * touch = [touches anyObject];
  self.viewOne.center = CGPointMake(self.viewOne.center.x+point.x - startLocation.x, self.viewOne.center.y);

if (startLocation.x < point.x) {
    //Swipe Right
    if ([self.photo.photoURLS isKindOfClass:[NSArray class]]) {
        if (self.isSwiping == NO) {
            self.isSwiping = YES;

            self.currentImage--;
            if (self.currentImage == -1) {
                self.currentImage = 12;
            }

            self.viewBack.frame = CGRectMake(-self.frame.size.width,0.0f, self.frame.size.width, self.frame.size.height);
            self.viewBack.hidden = NO;

            self.loadingImage.hidden = NO;
            NSURL *imageURL = [[NSURL alloc] initWithString:self.photo.photoURLS[self.currentImage]];
                [self asyncLoadImageFromURL:imageURL andPhotoID:self.photo.id withCallback:^(UIImage *image, NSString *photoID) {

                    self.loadingImage.hidden = YES;
                    [imageCache setImage:image forKey:self.photo.photoURLS[self.currentImage]];
                    self.viewBack.image = image;

                    //new
                        self.viewOne.frame = CGRectMake(0.0f, 0.0f, self.frame.size.width, self.frame.size.height);
                        self.viewOne.image = self.viewBack.image;

                        self.isSwiping = NO;
                        self.pageControl.currentPage = self.currentImage;
                        self.viewBack.hidden = YES;

                }];

            }
        }
    }


} else {
    //Swipe Left
   }

}

1 个答案:

答案 0 :(得分:0)

目前还不完全清楚你在问什么或问题是什么,但以下几行是可疑的:

CGPoint point = [[touches anyObject] locationInView:self.vehicleImage];
self.viewOne.center = CGPointMake(self.viewOne.center.x+point.x -startLocation.x, self.viewOne.center.y);

if (startLocation.x < endLocation.x)

在您触摸屏幕之前,您才设置endLocation。在touchesMoved响应者中,您将使用错误的endLocation数据。您应该在此函数中使用point的值来确定运动。

self.vehicleImageself.viewOne之间的关系也不是很清楚。如果self.vehicleImageself.viewOne的子视图(或者在您的帧更改期间移动),则locationInView将返回看似不一致的值,夸大手指的动作。相反,您应该选择一个在此过程中保持静态的视图(可能是您的ViewController的self.view)。