2016-08-23 10 views
0

我想實現我的應用程序如下:試圖實現的Facebook即時文章頁面,但我不能讓圖像視圖高度動畫

  1. 與圖像視圖滾動型和視圖子視圖。
  2. 當我從滾動視圖的頂部向下拖動時,圖像視圖在高度方面增加了方面填充模式,給人以縮放的印象。

  3. 當我放開時,圖像會回到原來的高度。

所以基本上,我試圖複製Facebook應用程序的即時文章頁面。

問題是我無法將圖像視圖恢復爲原始大小。它似乎總是反躬自問。 UIView.animateWithDuration()不在這裏工作因爲某些原因:/

這裏是我的代碼: (imageHeight是的UIImageView的高度約束的一個IBOutlet)

func scrollViewDidScroll(scrollView: UIScrollView) { 

    let y = self.scrollView.contentOffset.y 

    if y < 0 && self.imageHeight.constant < 500 
    { 
     self.imageHeight.constant += (-1*y)*0.5; 
    } 

} 

func scrollViewDidEndDecelerating(scrollView: UIScrollView) { 

    self.stoppedScrolling() 

} 


func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) { 


    if !decelerate 
    { 
     self.stoppedScrolling() 
    } 

} 

func stoppedScrolling() 
{ 

    UIView.animateWithDuration(1) { 

     self.imageHeight.constant = 180 

    } 

} 

Here's a gif of the result 而且,這不是」 t在gif中顯示,但在結束觸摸和調整大小之間存在半秒的延遲

回答

1

似乎是因爲UIImageView的高度限制導致的,我嘗試通過代碼創建UI並使用frame屬性來控制它,它可以工作。

對不起,我不知道怎麼寫SWIFT代碼,但我認爲我可以知道你的意思,你必須可以讀我的代碼爲好,像這樣:

#import "MainViewController.h" 

@interface MainViewController() 

@property UIScrollView *scrollView; 
@property UIImageView *imageView; 
@property bool needReceiveDraggingFlag; 

@end 

@implementation MainViewController 

- (void)viewDidLoad { 
[super viewDidLoad]; 
// Do any additional setup after loading the view. 

_scrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds]; 
_scrollView.backgroundColor = [UIColor grayColor]; 
_scrollView.delegate = self; 
_scrollView.scrollEnabled = YES; 
_scrollView.contentSize = CGSizeMake(_scrollView.frame.size.width,_scrollView.frame.size.height*1.2f); 
[self.view addSubview:_scrollView]; 

UILabel *lbInfo = [[UILabel alloc] initWithFrame:CGRectMake(50, 300, 200, 30)]; 
lbInfo.backgroundColor = [UIColor greenColor]; 
lbInfo.text = @"Test label"; 
[_scrollView addSubview:lbInfo]; 

_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; 
_imageView.backgroundColor = [UIColor redColor]; 
[self.view addSubview:_imageView]; 

_needReceiveDraggingFlag = YES; 
} 

- (void)didReceiveMemoryWarning { 
[super didReceiveMemoryWarning]; 
// Dispose of any resources that can be recreated. 
} 

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{ 
NSLog(@"scrollViewDidScroll"); 
if(_needReceiveDraggingFlag){ 
    float y = _scrollView.contentOffset.y; 
    if (y<0 && _imageView.frame.size.width<300) { 
     float nowImageLength = _imageView.frame.size.width; 
     float targetImageLength = nowImageLength + -y*0.5f; 
     _imageView.frame = CGRectMake(0, 0, targetImageLength, targetImageLength); 
    } 
} 
} 

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{ 
NSLog(@"scrollViewDidEndDragging"); 
[self stoppedScrolling]; 
} 

-(void)stoppedScrolling{ 
_needReceiveDraggingFlag = NO; 
[UIView animateWithDuration:1 animations:^{ 
    self.imageView.frame = CGRectMake(0, 0, 100, 100); 
} completion:^(BOOL isFinished){ 
    _needReceiveDraggingFlag = YES; 
}]; 
} 

@end 

希望它可以幫你。

+0

謝謝你的回答。它確實似乎在改變着高度。但是,圖像視圖的頂部不會被剪切到頂部邊距。任何方式來做到這一點? –

+0

我想我明白了。只需在scrollViewDidZoom()中添加self.imageView.center.y + = y即可達到所需的效果!你能編輯你的答案嗎? –

+0

我剛給你一些提示,如果你有你想要的,沒有理由編輯我的答案,很高興幫助你。 –

相關問題