0

我正在使用UIImageViewinUIScrollView来显示图片库。
这是我的代码:

    [scrollView1 setBackgroundColor:[UIColor blackColor]];
    [scrollView1 setCanCancelContentTouches:NO];
    scrollView1.indicatorStyle = UIScrollViewIndicatorStyleWhite;
    scrollView1.clipsToBounds = YES;
    scrollView1.scrollEnabled = YES;
    scrollView1.pagingEnabled = YES;


NSUInteger i;
for (i = 0; i <= kNumImages; i++)
{
    imageView.userInteractionEnabled = YES;

    imageName = [NSString stringWithFormat:@"image%d.jpg", i];
    UIImage *image = [UIImage imageNamed:imageName];
    imageView = [[UIImageView alloc] initWithImage:image];
    imageView.contentMode = UIViewContentModeScaleAspectFit;

    CGRect rect = imageView.frame;
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
    {
        rect.size.height = kScrollObjHeight;
        rect.size.width = kScrollObjWidth;

    }
    else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {
        rect.size.height = kScrollObjHeight_ipad;
        rect.size.width = kScrollObjWidth_ipad;
    }

    imageView.frame = rect;
    imageView.tag = i;
    [scrollView1 addSubview:imageView];
}  

现在我有一个保存按钮来将图像保存在库中。为此,我必须在滚动时找到每个图像标签。我认为可以通过检测滑动手势来实现UIImageView并且可以获取imageView.tag. 我搜索了很多东西,但没有得到太多的运气。我只知道很难识别滚动中的滑动。

我对这个手势的东西很陌生。谁能建议我该怎么做?
有没有其他方法可以获得特定的 imagetag ?或者,如果我只需要使用这个 Gesting Thins,那该怎么办?

任何帮助将是一个很大的帮助..!
谢谢你。

4

1 回答 1

2

在我看来,你有几个选择:

1.给你的UIImageView添加一个UISwipeGestureRecognizer,并在它的action方法中改变imageView的图像。例如

UISwipeGestureRecognizer *mySwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] init];
[mySwipeGestureRecognizer addTarget:self action:@selector(gestureAction:)];
[imageView addGestureRecognizer:mySwipeGestureRecognizer];

-(void)gestureAction:(id)sender  //Here you can also make some validations so to make sure the gesture is finished
{
imageView.image = [UIImage imageNamed:@"someOtherImage"];
}

然后只需保存 imageView 的图像。

2.另一种选择是根据图像的数量设置imageView的框架,然后简单地获取imageView的X坐标来计算它正在显示的图像。例如,

for(int i=0; i < numeberOfImages; i++)
{
imageView.frame = CGSizeMake(320*i,0,100,100);
[self.view addSubview:imageView];
}

然后获取 scoll 的位置以确定正在显示的图像。

另外,我认为另一种方法是实现scrollView的委托和方法

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

有一个变量来计算用户滚动了多少次,然后计算正在显示的图像。不要忘记设置

scrollView.delegate = self;
于 2012-08-23T09:18:38.823 回答