2013-10-22 89 views
3

我是iOS的初学者,所以我只是不确定在这里研究什么。我有一个UIScrollView添加了几个方形子视图。我怎样才能使子视图更小,因为他们滚动屏幕和更大,因为他们接近屏幕的中心?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 
+1

通过考虑你的目标在程序方面的含义来开始你的研究。 “我希望视图在离开屏幕时改变大小,这意味着它们的_position_将接近于零或与屏幕的大小接近,那么当我们移动时,我可以在哪里获得它们的位置?” –

回答

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 

这里是正在发生的事情的一点解释:

一个UIScrollView有几种特性允许您正确配置它。例如它有一个frame(灰色),它是从UIView继承的;使用此属性可以指定滚动视图的可见大小。它还有指定滚动视图总大小的contentSize(红色);在图像中显示为红色区域,但这仅适用于目的为在程序中它将不可见。将滚动视图的框架想象为一个窗口,让您只能看到滚动视图所包含的较大内容的一部分。

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

enter image description here

希望这有助于!

0

答案与分析的UIScrollView类Reference开始和它的delegate。在代理文档中,请参阅对滚动和拖动部分的响应。您还应该查看每个示例代码。您可以在子视图中创建销售点并更改uiview animation内的子视图属性。这些参考资料将为您了解可以在何处构建调用动画子视图的基础。

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

其他参考: UIKit ScrollViews

2

假设你有内部self.view了滚动,你可以在滚动视图delegate实施scrollViewDidScroll:发现当它滚动。

- (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 
    } 
} 

你可以使用框架来调整子视图的大小,只要你想。

相关问题