【问题标题】:Changing UIImageView height dynamically while scrolling滚动时动态更改 UIImageView 高度
【发布时间】:2014-07-20 01:35:28
【问题描述】:

我有一个与iPhone: Expand UITextView when scrolling 非常相似的问题

我的应用程序还应该在详细视图顶部有一个图片,在它下面有一个文本视图。滚动图片时应该缩小,以便为文本提供更多空间。可以在 Shazam 或 Rdio 等音乐应用中找到微笑效果,专辑封面位于顶部,专辑信息位于下方。

到目前为止,我设法通过使用- (void)scrollViewDidScroll:(UIScrollView *)scrollView 来调整图片大小,但它只有在 UIImageView 设置为 Aspect Fit 时才有效,但我需要它是 Aspect Fill,因为我不希望在调整框架大小时图片被挤压。

谁能帮我实现这个效果?

这是我目前使用的。我对编程很陌生,所以如果您对我编写的代码有任何其他的 cmet,我也非常感谢!

Viewcontroller.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController < UIScrollViewDelegate>


@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UITextView *textView;

@end

Viewcontroller.m

@interface ViewController ()

{
    CGFloat draggedOffsetY;
    CGFloat newHeight;
}

@end

@implementation ViewController
- (void)viewDidLoad
{
    [super viewDidLoad];

    //UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

    [self.scrollView setContentSize:CGSizeMake(320, 800)];
    CGPoint offset = CGPointMake(0, 0);
    [self.scrollView setContentOffset:offset];
    self.scrollView.delegate = self;  
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

    draggedOffsetY = self.scrollView.contentOffset.y; 
    NSLog(@"View was dragged to: %f", draggedOffsetY);
    newHeight = self.imageView.frame.size.height-draggedOffsetY;

    if (newHeight > 75 && newHeight < 150 ){
        [self.imageView setFrame:CGRectMake(0, 0, self.view.frame.size.width, newHeight)];
        CGFloat topCoordiate = self.imageView.frame.size.height;
        self.textView.frame = CGRectMake(0, topCoordinate, self.textView.frame.size.width, self.textView.frame.size.height);

    }

}

【问题讨论】:

    标签: ios cocoa-touch uiimageview resize scroll


    【解决方案1】:

    如果我没记错的话,我认为问题的原因是您还不了解“Aspect Fit”和“Aspect Fill”。设置为“Aspect Fill”,你必须同时改变imageView的宽度和高度来实现图像缩小。

    【讨论】:

    • 你可能是对的。所以你建议我再写几行代码来调整宽度,以适应纵横比?我不得不承认我认为它自己会这样做......我会试一试!
    • 其实我又想了一遍。我不想改变图片的宽度。我只希望 imageView 显示图像的一小部分。我将编辑我的问题以使其更易于理解。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-22
    相关资源
    最近更新 更多