2014-09-28 204 views
-1

我有UIView,在它的頂部,我有一個UIImageView,在底部UICollectionView。我的觀點是,當用戶向上滑動手指時,UIImageView框架應該輕輕移出(消失),並且當用戶向下滑動時,它應該再次出現(使用移動動畫)。以編程方式移動UIImageView框架

實際上,它工作,直到我設置圖像到UIImageView。舊代碼(下面介紹)沒有它工作得很好:

-(void)handleSwipeDown:(UISwipeGestureRecognizer *)recognizer { 

    NSLog(@"Swipe received."); 
    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:0.5]; 

    self.myCollectionView.frame = CGRectMake(0, 152, 320, 480); 
    [UIView commitAnimations]; 
} 

然後:

-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer { 

    NSLog(@"Swipe received."); 
    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:0.5]; 

    self.myCollectionView.frame=CGRectMake(0, 0, 320, 480); 

    [UIView commitAnimations]; 
} 

後我設置圖像:

UIImage *firstImage = [UIImage imageNamed:@"ipd1.jpg"]; 
    self.imageSlideshow.image = firstImage; 

以上代碼停止工作。 UIImage框架保持在同一個地方。 我怎麼能讓它移動? 任何意見,將不勝感激,謝謝!

回答

2

您應該停止使用beginAnimations:context:commitAnimations塊。從Apple的文檔:

在iOS 4.0及更高版本中不鼓勵使用此方法。您應該使用基於塊的動畫方法來指定您的動畫。

使用這個代替:

[UIView animateWithDuration:0.5 animations:^{ 
    self.myCollectionView.frame = CGRectMake(0, 152, 320, 480); 
}]; 

就上述替換整個swipeDown方法。

+0

謝謝,但它不工作,圖像仍然在這裏。 – 2014-09-28 08:54:59

+0

在代碼示例中,您正在移動myCollectionView的框架,但我認爲您還需要移動imageSlideshow以及? – believesInSanta 2014-09-28 08:57:09

+0

那麼,其實在我的代碼imageView是不可見的,但在面對面,它的位置並沒有改變。我只是將collectionView的其餘部分「拉」起來(因爲在屏幕下方,它是隱形的)。 – 2014-09-28 09:03:54

相關問題