滚动后无法在滚动视图中设置子视图的帧

时间:2014-06-16 10:18:16

标签: ios uiscrollview subview

我有一个仅限水平的UIScrollView。 它包含几个UIImageViews和一个borderView,用于指示选择哪一个。

borderView是一个带边框但没有内容的UIView。

我想做什么:

当用户点击imageView时,我希望borderview可以移动到并覆盖在tapped imageView上进行指示。

enter image description here

我在代码中所做的事情:

1.将带有UITapgesture事件和borderView的imageViews添加到scrollView

-(void)setStaticFilterToBar
{
    _filterList = [APIHelper getStaticFilterList:_originBackgroundImage];
    filterScrollView.contentSize = CGSizeMake(320,filterScrollView.contentSize.height);
    filterScrollView.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.7f];
    int xAis = 64;
    for(int i=0; i<_filterList.count; i++)
    {
        UIImageView *filter = [[UIImageView alloc]initWithImage:[_filterList objectAtIndex:i]];
        [filter setUserInteractionEnabled:YES];
        [filter setFrame:CGRectMake(i * xAis,5,60,60)];
        [filter setTag:i];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self 
action:@selector(filterElementTapToApplyFilter:)];
        [tap setDelegate:self];
        [filter addGestureRecognizer:tap];
        [filterScrollView addSubview:filter];
        if((i+1) * xAis > 320)
        {
            filterScrollView.contentSize = CGSizeMake(filterScrollView.contentSize.width + xAis,
                                               filterScrollView.contentSize.height);
        }
    }

    //add borderview
    UIView *borderView = [[UIView alloc]init];
    borderView.layer.borderColor = [UIColor redColor].CGColor;
    borderView.layer.borderWidth = 3.0f;
    borderView.layer.cornerRadius = 6;
    [filterScrollView addSubview:borderView];
}

-(void)filterElementTapToApplyFilter:(UITapGestureRecognizer *) recognizer
{
    //apply filter
    [self applyFilterByUIView:recognizer.view];

    //move the borderview to the tapped imageView
    [self selectSubViewsFromScrollView:filterScrollView TargetFrame:recognizer.view.frame];
}

2.点击图像视图,将borderview的帧值更改为imageView&#39; s。 (无论使用animationwithDuration:animations:completion:还是直接设置框架)

-(void)selectSubViewsFromScrollView:(UIScrollView *)scrollView TargetFrame:(CGRect)targetFrame
{
    //borderView is the last subview.
    UIView *borderView = [scrollView.subviews objectAtIndex:scrollView.subviews.count-1];

    NSLog(@"Before: borderView.frame:(%d,%d,%d,%d)",(int)borderView.frame.origin.x,
                (int)borderView.frame.origin.y,
                (int)borderView.frame.size.width,
                (int)borderView.frame.size.height);
    //1
    borderView.frame = targetFrame;

    //2 for animation
    //[UIView animateWithDuration:0.3 animations:^{
    //    borderView.frame = targetFrame;
    //}];
    NSLog(@"After: borderView.frame:(%d,%d,%d,%d)",(int)borderView.frame.origin.x,
                (int)borderView.frame.origin.y,
                (int)borderView.frame.size.width,
                (int)borderView.frame.size.height);
}

我遇到的问题:

它的效果就像我预测的那样开始。

但是在滚动filterScrollView之后,点击imageview并且borderview不再改变它的位置。但仍然正确应用正确的过滤器。

borderview框架的值已更改,但屏幕中的位置未发生变化。

这里发生了什么?我错过了什么吗?任何帮助将不胜感激。

注意。我使用故事板并对所有视图使用无自动布局。

1 个答案:

答案 0 :(得分:0)

我们可以使用Collection View,集合视图在内部也可以像滚动视图一样工作,我们也可以轻松处理选择项目。

在cellforItemAtIndexPath方法中添加如下所示的选定和正常单元格

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView        cellForItemAtIndexPath:(NSIndexPath *)indexPath
     {

     UICollectionViewCell *cell = [collectionView     dequeueReusableCellWithReuseIdentifier:@"collectionViewCell" forIndexPath:indexPath];

  if (cell.selected) {
    cell.backgroundColor = [UIColor blueColor]; // highlight selection 
   }
   else
  {
    cell.backgroundColor = [UIColor clearColor]; // Default color
   }
 return cell;
 }

//选择它后添加颜色

  -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath  *)indexPath  {

       UICollectionViewCell *datasetCell =[collectionView cellForItemAtIndexPath:indexPath];
       datasetCell.backgroundColor = [UIColor blueColor]; // highlight selection
     }  

//取消选择时将其设为正常样式

 -(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {

      UICollectionViewCell *datasetCell =[collectionView cellForItemAtIndexPath:indexPath]; 
      datasetCell.backgroundColor = [UIColor ClearColor]; // Default color
  }