ios - 尝试实现 Facebook 即时文章页面,但我无法获取 ImageView 高度以进行动画处理

标签 ios swift uiscrollview uiimageview

我正在尝试在我的应用中实现以下内容:

  1. 一个带有 ImageView 和作为 subview 的 View 的 ScrollView 。
  2. 当我从 ScrollView 的顶部向下拖动时, ImageView 的高度会增加,在纵横比填充模式下,给人一种缩放的感觉。

  3. 当我放手时,图像会恢复到原来的高度。

基本上,我正在尝试复制 Facebook 应用程序的即时文章页面。

问题是我无法通过动画使 ImageView 恢复到其原始大小。它似乎总是猛然回到它身边。 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 此外,它没有显示在 gif 中,但在结束触摸和调整大小之间有半秒的延迟

最佳答案

好像是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

希望对您有所帮助。

关于ios - 尝试实现 Facebook 即时文章页面,但我无法获取 ImageView 高度以进行动画处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39106246/

相关文章:

ios - 错误 : ld: library not found for -lPods with CocoaPods

ios - 将ScrollView添加到UIView中

ios - 使用Interface Builder在iOS中一页页面上的视差标题

ios - PickerIOS 一直显示在 React-Native 中

ios - 在 Swift 中从另一个 ViewController 访问变量

ios - UserDefaults 在同一应用组内具有不同的值

ios - 如何快速从结构中检索值

ios - 需要帮助将新的存储属性添加到 UITableView

ios - watchOs 上的通知

ios - UIScrollView 初始化时的内容偏移