首先實現UIScrollViewDelegate協議:
[plain] view plaincopy
1 #import
2
3 @interface Activity01ViewController : UIViewController
4 UIScrollView *myscrollview;
5 }
6 @property (nonatomic,retain) UIScrollView *myscrollview;
7 @end
實現協議的下列方法:
[plain] view plaincopy
8 #import
9
10 @interface Activity01ViewController : UIViewController
11 UIScrollView *myscrollview;
12 }
13 @property (nonatomic,retain) UIScrollView *myscrollview;
14 @end
對myscrollview進行初始化:
[plain] view plaincopy
15 - (void)viewDidLoad
16 {
17 [super viewDidLoad];
18 // Do any additional setup after loading the view, typically from a nib.
19 myscrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0, 44,768, 1004)];
20 myscrollview.directionalLockEnabled = YES; //只能一個方向滑動
21 myscrollview.pagingEnabled = NO; //是否翻頁
22 myscrollview.backgroundColor = [UIColor blackColor];
23 myscrollview.showsVerticalScrollIndicator =YES; //垂直方向的滾動指示
24 myscrollview.indicatorStyle = UIScrollViewIndicatorStyleWhite;//滾動指示的風格
25 myscrollview.showsHorizontalScrollIndicator = NO;//水平方向的滾動指示
26 myscrollview.delegate = self;
27 CGSize newSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height+1);
28 [myscrollview setContentSize:newSize];
29
30 float x=0;
31 float y=0;
32 float width =768;
33 float height = 1004;
34
35 UIImageView *myimage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Activity01.png"]];
36 //[myimage setContentMode:UIViewContentModeScaleAspectFill];
37 [myimage setFrame:CGRectMake(x, y, width, height)];
38 [myscrollview addSubview:myimage];
39
40
41 [self.view addSubview:myscrollview];
42 }
屬性總結:
屬性 作用
CGPoint contentOffSet 監控目前滾動的位置
CGSize contentSize 滾動范圍的大小
UIEdgeInsets contentInset 視圖在scrollView中的位置
id
BOOL directionalLockEnabled 指定控件是否只能在一個方向上滾動
BOOL bounces 控制控件遇到邊框是否反彈
BOOL alwaysBounceVertical 控制垂直方向遇到邊框是否反彈
BOOL alwaysBounceHorizontal 控制水平方向遇到邊框是否反彈
BOOL pagingEnabled 控制控件是否整頁翻動
BOOL scrollEnabled 控制控件是否能滾動
BOOL showsHorizontalScrollIndicator 控制是否顯示水平方向的滾動條
BOOL showsVerticalScrollIndicator 控制是否顯示垂直方向的滾動條
UIEdgeInsets scrollIndicatorInsets 指定滾動條在scrollerView中的位置
UIScrollViewIndicatorStyleindicatorStyle 設定滾動條的樣式
float decelerationRate 改變scrollerView的減速點位置
BOOL tracking 監控當前目標是否正在被跟蹤
BOOL dragging 監控當前目標是否正在被拖拽
BOOL decelerating 監控當前目標是否正在減速
BOOL delaysContentTouches 控制視圖是否延時調用開始滾動的方法
BOOL canCancelContentTouches 控制控件是否接觸取消touch的事件
float minimumZoomScale 縮小的最小比例
float maximumZoomScale 放大的最大比例
float zoomScale 設置變化比例
BOOL bouncesZoom 控制縮放的時候是否會反彈
BOOL zooming 判斷控件的大小是否正在改變
BOOL zoomBouncing 判斷是否正在進行縮放反彈
BOOL scrollsToTop 控制控件滾動到頂部
注意:
1、使scrollview不要上下滑動,可以設置
scrollview.contentSize = CGSizeMake(你要的長度, 0);
協議方法解釋:
[plain] view plaincopy
43 #pragma mark UIScrollViewDelegate
44 //只要滾動了就會觸發
45 - (void)scrollViewDidScroll:(UIScrollView *)scrollView;
46 {
47 // NSLog(@" scrollViewDidScroll");
48 NSLog(@"ContentOffset x is %f,yis %f",scrollView.contentOffset.x,scrollView.contentOffset.y);
49 }
50 //開始拖拽視圖
51 - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView;
52 {
53 NSLog(@"scrollViewWillBeginDragging");
54 }
55 //完成拖拽
56 - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate;
57 {
58 NSLog(@"scrollViewDidEndDragging");
59 }
60 //將開始降速時
61 - (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView;
62 {
63 NSLog(@"scrollViewWillBeginDecelerating");
64 }
65
66 //減速停止了時執行,手觸摸時執行執行
67 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;
68 {
69 NSLog(@"scrollViewDidEndDecelerating");
70 }
71 //滾動動畫停止時執行,代碼改變時出發,也就是setContentOffset改變時
72 - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView;
73 {
74 NSLog(@"scrollViewDidEndScrollingAnimation");
75 }
76 //設置放大縮小的視圖,要是uiscrollview的subview
77 - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView;
78 {
79 NSLog(@"viewForZoomingInScrollView");
80 return viewA;
81 }
82 //完成放大縮小時調用
83 - (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale;
84 {
85 viewA.frame=CGRectMake(50,0,100,400);
86 NSLog(@"scale between minimum and maximum. called after any 'bounce' animations");
87 }// scale between minimum and maximum. called after any 'bounce' animations
88
89 //如果你不是完全滾動到滾軸視圖的頂部,你可以輕點狀態欄,那個可視的滾軸視圖會一直滾動到頂部,那是默認行為,你可以通過該方法返回NO來關閉它
90 - (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView;
91 {
92 NSLog(@"scrollViewShouldScrollToTop");
93 returnYES;
94 }
95
96 - (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView;
97 {
98 NSLog(@"scrollViewDidScrollToTop");
99 }