3

我是 iOS 的初学者,所以我只是不确定要在这里研究什么。我有一个添加了几个方形子视图的 UIScrollView。我怎样才能使子视图在它们滚出屏幕时变小,当它们接近屏幕中心时变大?

#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
4

3 回答 3

5

在这里,您可以找到您正在尝试完成的工作的完整示例。它只有一个子视图,因为它只是为了让您了解如何完成它。此外,此示例在 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

这里是发生了什么的一点解释:

AUIScrollView有几个属性可以让您正确配置它。例如,它有一个frame(gray) 继承自UIView; 使用此属性,您可以指定滚动视图的可见大小。它还有contentSize(red) 指定滚动视图的总大小;在图像中它显示为红色区域,但这仅用于说明目的,因为它在程序中不可见。将滚动视图的框架想象成一个窗口,让您只看到滚动视图具有的较大内容的一部分。

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

在此处输入图像描述

希望这可以帮助!

于 2013-10-22T21:12:17.087 回答
2

假设你里面有scrollView ,你可以在scrollviewself.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
    }
}

您可以使用框架根据需要调整子视图的大小。

于 2013-10-22T20:24:50.880 回答
0

答案从分析 UIScrollView 类参考和它的委托开始。在委托文档中,请参阅响应滚动和拖动部分。您还应该查看每个示例代码。您可以为您的子视图创建出口并更改uiview 动画中的子视图属性。这些参考资料将为您了解可以在何处构建调用以动画子视图提供良好的基础。

这是动画子视图的链接。可以通过谷歌搜索“uiview subview animation”(不带引号)找到其他示例。如果您遇到任何重大问题,请先阅读头文件并发布一些示例代码以获得额外(更精确)的帮助。

其他参考: UIKit ScrollViews

于 2013-10-22T20:16:48.297 回答