简体   繁体   English

UIScrollview - 如何在滚动离开屏幕时使子视图更小

[英]UIScrollview - how to make subview smaller as it scrolls off screen

I'm a beginner with iOS, so i'm just not sure what to research here. 我是iOS的初学者,所以我不确定在这里研究什么。 I have a UIScrollView with a few square subViews added. 我有一个UIScrollView,添加了几个方形子视图。 How can i make the subviews smaller as they scroll off screen and bigger as they approach the center of the screen? 当他们接近屏幕中心时,如何使子视图在屏幕上滚动时更小?

#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

Here you can find a fully working example of what you're trying to accomplish. 在这里,您可以找到您正在尝试完成的完整工作示例。 It only has one subview because it's just to give you an idea of how can you accomplish it. 它只有一个子视图,因为它只是让你知道如何实现它。 Also, this example was tested on an iPad (iOS7) simulator. 此外,此示例在iPad(iOS7)模拟器上进行了测试。

The *.h file * .h文件

#import <UIKit/UIKit.h>

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

@property (nonatomic, strong) UIView *squareView;

@end

The *.m file * .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

And here is a little explanation of what is going on: 以下是对正在发生的事情的一点解释:

A UIScrollView has several properties that allow you to configure it correctly. UIScrollView有几个属性,允许您正确配置它。 For example it has a frame (gray) which is inherited from UIView ; 例如,它有一个frame (灰色),它继承自UIView ; with this property you specify the visible size of the scroll view. 使用此属性指定滚动视图的可见大小。 It also has the contentSize (red) which specifies the total size of the scroll view; 它还有contentSize (红色),它指定了滚动视图的总大小; in the image it's showed as the red area but this is only for illustration purposes as it will not be visible in the program. 在图像中,它显示为红色区域,但这仅用于说明目的,因为在程序中不可见 Imagine the scroll view's frame as the window that let's you see only a part of the bigger content the scroll view has. 想象一下滚动视图的框架作为窗口,让您只看到滚动视图所具有的较大内容的一部分。

When the user starts scrolling a gap appears between the top part of the contentSize and the top part of the frame. 当用户开始滚动时,在contentSize的顶部和框架的顶部之间出现间隙。 This gap is known as the contentOffset 这个差距称为contentOffset

在此输入图像描述

Hope this helps! 希望这可以帮助!

Assuming that you have the scrollView inside self.view , you can implement scrollViewDidScroll: in the scroll view delegate to find when it is scrolled. 假设你有里面的滚动视图self.view ,可以实现scrollViewDidScroll:在滚动视图delegate找到当它滚动。

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    for (UIView *view in self.scrollView.subviews) {
        CGRect frame = [view convertRect:view.frame toView:self.view]; // Contains the frame of view with respect to self.view
    }
}

You can them use the frame to resize subviews as you want. 您可以根据需要使用框架调整子视图的大小。

The answer starts with analyzing the UIScrollView Class Reference and it's delegate . 答案从分析UIScrollView类参考及其委托开始 In the delegate documentation see the responding to scrolling and dragging section. 在委托文档中,请参阅响应滚动和拖动部分。 You should also review the sample code for each. 您还应该查看每个示例代码。 You can create outlets to your subviews and change the subview properties within a uiview animation . 您可以为子视图创建出口并更改uiview动画中的子视图属性。 These references will give you a good foundation in understanding where you can build the call to animate the subviews. 这些参考资料将为您提供一个良好的基础,帮助您了解在哪里构建调用动画子视图的调用。

Here is a link to animating subviews . 这是动画子视图的链接。 Additional examples can be found by Googling "uiview subview animation" (without the quotes). 其他示例可以通过Google搜索“uiview子视图动画”(不带引号)找到。 If you run into any major issues read the header files first and post some sample code for additional (more precise) help. 如果遇到任何重大问题,请首先阅读头文件并发布一些示例代码以获得更多(更精确)的帮助。

Other reference: UIKit ScrollViews 其他参考: UIKit ScrollViews

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM