我正在尝试在我的应用中实现以下内容:
- 一个带有 ImageView 和作为 subview 的 View 的 ScrollView 。
当我从 ScrollView 的顶部向下拖动时, ImageView 的高度会增加,在纵横比填充模式下,给人一种缩放的感觉。
当我放手时,图像会恢复到原来的高度。
基本上,我正在尝试复制 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/