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

ios – UIScrollview – 如何在滚动离开屏幕时使子视图更小,第1张

概述我是iOS的初学者,所以我不确定在这里研究什么.我有一个UIScrollView,添加了几个方形子视图.当他们接近屏幕中心时,如何使子视图在屏幕上滚动时更小? #import "HorizontalScrollMenuViewController.h"#import <UIKit/UIKit.h>#define SUBVIEW_WIDTH_HEIGHT 280@interface Hori 我是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,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

希望这可以帮助!

总结

以上是内存溢出为你收集整理的ios – UIScrollview – 如何在滚动离开屏幕时使子视图更小全部内容,希望文章能够帮你解决ios – UIScrollview – 如何在滚动离开屏幕时使子视图更小所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1018621.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存