Reputation: 399
I'm trying to implement the following in my app:
As I drag down from the top of the scroll view, the image view increases in height, in the aspect fill mode, giving the impression of zooming.
When I let go, the image returns to its original height.
So basically, I'm trying to replicate the Facebook app's instant article page.
The problem is that I can't animate the image view returning to its original size. It seems to always jerk back to it. UIView.animateWithDuration() doesn't work here for some reason:/
here is my code: (imageHeight is an IBOutlet of the UIImageView's height constraint)
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 Also, it doesn't show in the gif but there's a half-second delay between end touches and resizing
Upvotes: 0
Views: 88
Reputation: 1294
Seems like it cause by UIImageView's height constraint, I try to create the UI by code and use frame property to control it, it works.
Sorry about that I don't know how to write swift code, but I think that I can know what you mean, you must can read my code as well, like this:
#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
Hope it can help you.
Upvotes: 1