2013-10-22 52 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 
    } 
} 

你可以使用框架來調整子視圖的大小,只要你想。

相關問題