2014-06-16 71 views
0

我有一个水平只有UIScrollView。它包含几个UIImageViews和一个用于指示选择哪一个的边框视图。滚动后无法在滚动视图中设置子视图的框架

borderView是边界而没有内容的UIView。

我想做的事:

当用户敲击ImageView的,我希望borderview可以移动并覆盖在挖掘ImageView的指示。

enter image description here

我在我的代码所做的:

1.增加imageViews与UITapgesture事件和borderView到了滚动

-(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的ImageView的改变borderview的frameView的值与imageView的相同。 (无论使用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不会改变其位置anymore.but仍然正确应用适当的过滤器。

borderview的帧的值被改变,但在屏幕上的位置没有改变。

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

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

+0

为什么不向UIImageViews图层添加边框? –

+0

因为我希望边界从这个图像视图移动到另一个动画。我不确定添加.layer.boder可能会产生这种效果。 – superwave

+0

你提供了什么坐标来使frameView在滚动后移动?坐标应该相对于scrollView的内容大小而不是scrollView的框架 – NavinDev

回答

0

我们可以使用集合视图,集合视图也在内部工作一样滚动视图,也是我们可以很容易地处理选择项目。

在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 
    } 
相关问题