scrollview的delegate與files owner關聯
.h文件
#import <UIKit/UIKit.h>
@interface EXSVViewController : UIViewController<UIScrollViewDelegate>
@end
.m文件
//
// EXSVViewController.m
// ExerciseScrollView
//
// Created by hxl on 13-5-21.
// Copyright (c) 2013年 xiaolei.hu. All rights reserved.
//
#import "EXSVViewController.h"
@interface EXSVViewController ()
@property (nonatomic) IBOutlet UIScrollView *scrollView;//在xib中與scrollview控件關聯
@end
@implementation EXSVViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Scroll view
UInt16 h = 6;
UInt16 w = 6;
for (UInt16 i = 0; i < h; i++) {
//CGRectMake(X軸座標,Y軸座標,寬,高)
for (UInt16 j = 0; j < w; j++) {
//設置圖片位置
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(self.scrollView.frame.size.width * i, self.scrollView.frame.size.height * j, self.scrollView.frame.size.width,self.scrollView.frame.size.height)];
// 指定URL生成UIImage
[button setTitle:@"按鈕" forState:UIControlStateNormal]; //設置button的標題
//[button addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside]; //定義點擊時的響應函數
[button setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",i+1]] forState:UIControlStateNormal];
[self.scrollView addSubview:button];
}
}
//NSLog(@"%@",self);
//等同於在.xlb文件中delegate和File's Owner連接
//[self.scrollView setDelegate:self];
[self.scrollView setContentSize:CGSizeMake(self.scrollView.frame.size.width * h, self.scrollView.frame.size.height * w)];
//[self.scrollView setDirectionalLockEnabled:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
typedef enum {
UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait,
UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight,
UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft
} UIInterfaceOrientation;
*/
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);//系統默認不支持旋轉功能
//返回YES屏幕所有方向旋轉都支持
//return YES;
}
// 觸摸屏幕來滾動畫面還是其他的方法使得畫面滾動,皆觸發該函數
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
NSLog(@"Scrolling...");
}
// 觸摸屏幕並拖拽畫面,再鬆開,最後停止時,觸發該函數
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
NSLog(@"scrollViewDidEndDragging - End of Scrolling.");
}
// 滾動停止時,觸發該函數
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
NSLog(@"scrollViewDidEndDecelerating - End of Scrolling.");
}
// 調用以下函數,來自動滾動到想要的位置,此過程中設置有動畫效果,停止時,觸發該函數
// UIScrollView的setContentOffset:animated:
// UIScrollView的scrollRectToVisible:animated:
// UITableView的scrollToRowAtIndexPath:atScrollPosition:animated:
// UITableView的selectRowAtIndexPath:animated:scrollPosition:
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
NSLog(@"scrollViewDidEndScrollingAnimation - End of Scrolling.");
}
@end