星期四, 2月 02, 2012

ios iphone/ipad Dev 學習筆記 (A-08) UIScrollView


環境:Xcode 4.2 + iOS SDK 5
目標:
  • 練習使用 UIScrollView
在這個練習我建立一個新的專案,並且建立一個 ViewController,然後在這個 View 裡面配置 ScrollView

  
HomeViewCtrl.h

  #import <UIKit/UIKit.h>

  @interface HomeViewCtrl : UIViewController

  @property(nonatomic, strong) UIScrollView * scrollView;
  @property(nonatomic, strong) UILabel * label;

  @end



HomeViewCtrl.m
  #import "HomeViewCtrl.h"

  @implementation HomeViewCtrl

  @synthesize scrollView, label;

  - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  {
      self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
      if (self) {
          // Custom initialization
      }
      return self;
  }

  - (void)didReceiveMemoryWarning
  {
      // Releases the view if it doesn't have a superview.
      [super didReceiveMemoryWarning];
    
      // Release any cached data, images, etc that aren't in use.
  }

  // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
  - (void)viewDidLoad
  {
      //取得螢幕的尺寸
      //bounds = 螢幕範圍
      //applicationFrame = bounds 扣除狀態列佔據的空間
      CGRect screenRect = [[UIScreen mainScreen] applicationFrame];     
      CGFloat screenWidth = screenRect.size.width;
      CGFloat screenHeight = screenRect.size.height;

      //配置 scrollView
      //scrollView 的尺寸跟螢幕一樣大,但是內容在高度的方向加大
      scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
      scrollView.contentSize = CGSizeMake(screenWidth, (screenHeight + 300));
      scrollView.scrollEnabled = YES;
    
      [self.view addSubview:scrollView];
    
      //配置一個 label,以便提供視覺參考
      label = [[UILabel alloc] initWithFrame:CGRectMake(80, 100, 100, 50)];
      label.text = @"ScrollView Test";
      [self.scrollView addSubview:label];

      [super viewDidLoad];
}

  - (void)viewDidUnload
  {
      [super viewDidUnload];
      // Release any retained subviews of the main view.
      // e.g. self.myOutlet = nil;
  }

  - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  {
      // Return YES for supported orientations
      return (interfaceOrientation == UIInterfaceOrientationPortrait);
  }

  @end



沒有留言: