ios - UIScrollview - 如何在滚动屏幕时使 subview 变小

标签 ios iphone objective-c uiscrollview

我是 iOS 的初学者,所以我不确定要在这里研究什么。我有一个添加了几个方形 subview 的 UIScrollView。如何让 subview 在滚动出屏幕时变小,在接近屏幕中心时变大?

#import "HorizontalScrollMenuViewController.h"
#import <UIKit/UIKit.h>

#define SUBVIEW_WIDTH_HEIGHT 280
@interface HorizontalScrollMenuViewController : UIViewController

@property (nonatomic, strong) IBOutlet UIScrollView *scrollView;

@end


@implementation HorizontalScrollMenuViewController

-(void)viewDidLoad{
    [super viewDidLoad];

    NSArray *colors = [NSArray arrayWithObjects:[UIColor greenColor],[UIColor redColor],[UIColor orangeColor],[UIColor blueColor],nil ];
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    CGFloat screenWidth = screenRect.size.width;
    CGFloat screenHeight = screenRect.size.height;
    CGFloat originX = (screenWidth - SUBVIEW_WIDTH_HEIGHT)/2.0; // get margin to left and right of subview
    CGFloat originY = ((screenHeight - SUBVIEW_WIDTH_HEIGHT)/2);


    // add subviews of all activities
    for (int i = 0; i < colors.count; i++){

        CGRect frame = CGRectMake(0,0,SUBVIEW_WIDTH_HEIGHT,SUBVIEW_WIDTH_HEIGHT);
        frame.origin.x = self.scrollView.frame.size.width * i + originX;
        frame.origin.y = originY;
        UIView *subView = [[UIView alloc] initWithFrame:frame];

        [UIView setAnimationBeginsFromCurrentState: YES];
        subView.layer.cornerRadius = 15;
        subView.layer.masksToBounds = YES;

        subView.backgroundColor = [colors objectAtIndex:i];

        [self.scrollView addSubview:subView];
    }

    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * colors.count, self.scrollView.frame.size.height);
}

@end

最佳答案

在这里,您可以找到一个完整的示例来说明您要完成的任务。它只有 一个 subview ,因为它只是让您了解如何完成它。此外,此示例还在 iPad (iOS7) 模拟器上进行了测试。

*.h文件

#import <UIKit/UIKit.h>

// Remember to declare ourselves as the scroll view delegate
@interface TSViewController : UIViewController <UIScrollViewDelegate>

@property (nonatomic, strong) UIView *squareView;

@end

*.m 文件

#import "TSViewController.h"

@implementation TSViewController
@synthesize squareView = _squareView;

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Create and configure the scroll view (light gray)
    UIScrollView *myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(100, 100, 500, 500)];
    CGRect contentSize = myScrollView.frame;
    contentSize.size.height = contentSize.size.height + 400;
    myScrollView.contentSize = contentSize.size;
    myScrollView.userInteractionEnabled = YES;

    // give the scroll view a gray color so it's easily identifiable
    myScrollView.backgroundColor = [UIColor lightGrayColor];

    // remember to set yourself as the delegate of the scroll view
    myScrollView.delegate = self;

    [self.view addSubview:myScrollView];

    // Create and configure the square view (blue)
    self.squareView = [[UIView alloc] initWithFrame:CGRectMake(200, 400, 60, 60)];
    self.squareView.backgroundColor = [UIColor blueColor];
    [myScrollView addSubview:self.squareView];
}

// Here is where all the work happens
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {

    // Get the difference between the contentOffset y position and the squareView y position
    CGFloat y = self.squareView.frame.origin.y - scrollView.contentOffset.y;

    // If the square has gone out of view, return
    if (y <= 0) {
        return;
    }

    // Modify the squareView's frame depending on it's current position
    CGRect squareViewFrame = self.squareView.frame;
    squareViewFrame.size.height = y + 5;
    squareViewFrame.size.width = y + 5;
    squareViewFrame.origin.x = (scrollView.contentSize.width - squareViewFrame.size.width) / 2.0;
    self.squareView.frame = squareViewFrame;
}

@end

下面是对正在发生的事情的一些解释:

UIScrollView 有几个允许您正确配置它的属性。例如它有一个继承自UIViewframe(灰色);使用此属性,您可以指定 ScrollView 的可见大小。它还具有 contentSize(红色),用于指定 ScrollView 的总大小;在图像中它显示为红色区域,但这仅用于说明目的,因为它不会在程序中可见。将 ScrollView 的框架想象成一个窗口,让您只能看到 ScrollView 所具有的更大内容的一部分。

当用户开始滚动时,contentSize 的顶部和框架的顶部之间会出现一个间隙。此间隙称为 contentOffset

enter image description here

希望这对您有所帮助!

关于ios - UIScrollview - 如何在滚动屏幕时使 subview 变小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19526939/

相关文章:

ios - iOS 7 状态栏中的 MFMailComposeViewController 是黑色的

iPhone内存过度释放

iphone - 如何测试 NSString 是否为 nil?

iphone - 在边界内倾斜移动图像

在比 SDK 更新的 iOS 上进行 iPhone IOS 设备测试

ios - 无法在 Xcode 中分发适用于 iOS 的 Flutter 应用程序 - "ipa in the package contains an invalid character(s)"

iPhone CGContext : drawing two lines with two different colors

ios - 如何解决 uitableviewcell 重用的自定义子类的问题?

ios - 如何使用 NSURLRequest 设置 Cookie?

objective-c - ALAsset 不可编辑