2014-06-16 70 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 
    } 
相關問題