【问题标题】:Trying to implement Facebook instant article page but I can't get the image view height to animate试图实现 Facebook 即时文章页面,但我无法让图像视图高度动画化
【发布时间】:2016-12-30 13:57:42
【问题描述】:

我正在尝试在我的应用中实现以下功能:

  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 此外,它没有显示在 gif 中,但在结束触摸和调整大小之间有半秒的延迟

【问题讨论】:

    标签: ios swift uiscrollview uiimageview


    【解决方案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
    

    希望对你有帮助。

    【讨论】:

    • 感谢您的回答。它似乎确实为不断变化的高度设置了动画。但是,图像视图的顶部不会一直被剪裁到上边距。有什么办法吗?
    • 我想我明白了。刚刚在scrollViewDidZoom()中添加了self.imageView.center.y += y,就达到了需要的效果!你能编辑你的答案吗??
    • 我只是给了你一些提示,如果你有你想要的,没有理由编辑我的答案,很高兴帮助你。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多