我是iOS的初学者,所以我不确定在这里研究什么.我有一个UIScrollView,添加了几个方形子视图.当他们接近屏幕中心时,如何使子视图在屏幕上滚动时更小? #import "HorizontalScrollMenuViewControlle
#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在这里,您可以找到您正在尝试完成的完整工作示例.它只有
一个子视图,因为它只是让你知道如何实现它.此外,此示例在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有几个属性,允许您正确配置它.例如,它有一个框架(灰色),它继承自UIView;使用此属性指定滚动视图的可见大小.它还有contentSize(红色),它指定了滚动视图的总大小;在图像中,它显示为红色区域,但这仅用于说明目的,因为它在程序中不可见.想象一下滚动视图的框架作为窗口,让您只看到滚动视图所具有的较大内容的一部分.
当用户开始滚动时,在contentSize的顶部和框架的顶部之间出现间隙.这个差距称为contentOffset
> Here is the reference to UIScrollView
> Here is the reference to UIScrollViewDelegate
希望这可以帮助!