简体   繁体   English

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

[英]can't set subview's frame in scrollview after scrolling

I have an Horizontal-only UIScrollView. 我有一个仅限水平的UIScrollView。 it contains several UIImageViews and a borderView for indicating which one is selected. 它包含几个UIImageViews和一个borderView,用于指示选择了哪个。

borderView is a UIView with border and no content. borderView是一个带边框且没有内容的UIView。

what I want to do: 我想做的事:

when the user tap the imageView, I wish the borderview can move to and overlay on the tapped imageView for indicating. 当用户点击imageView时,希望borderview可以移动到并覆盖在所点击的imageView上以进行指示。

在此处输入图片说明

what I did in my code: 我在代码中所做的:

1.Add imageViews with UITapgesture event and borderView to the scrollView 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.Tap the imageview to change the borderview's frame value to that of imageView's. 2.点击imageview,将borderview的帧值更改为imageView的帧值。 (no matter using animationwithDuration:animations:completion: or set the frame directly) (无论使用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);
}

The problem I have: 我有的问题:

it works just fine as I predicted for beginning. 正如我最初预测的那样,它的工作正常。

But after scrolling the filterScrollView, click on imageview and the borderview won't change its position anymore.but still apply the right filter correctly. 但是在滚动filterScrollView之后,单击imageview,borderview将不再更改其位置。但是仍然正确应用正确的滤镜。

the value of borderview's frame is changed, but the position in screen didn't change. borderview的frame的值已更改,但屏幕中的位置未更改。

what happened here? 这里发生了什么? Did I miss anything? 我有想念吗? Any help would be appreciated. 任何帮助,将不胜感激。

Note. 注意。 I use storyboard and use No autolayout for all views. 我使用情节提要,并为所有视图使用无自动布局。

We can use Collection View ,collection view also internally work same as scroll view and also we can handle selecting items easily . 我们可以使用收藏夹视图,收藏夹视图在内部也可以像滚动视图一样工作,并且我们可以轻松地选择项目。

in cellforItemAtIndexPath method add selected and normal cells like below 在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;
 }

//once it is selected add the color //一旦选择添加颜色

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

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

//when deselected make it as normal style //当取消选中时将其设为普通样式

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM