博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

UIScrollView 用法

Posted on 2011-08-26 00:06  星尘的天空  阅读(1747)  评论(0编辑  收藏  举报

m_Scroll.frame = CGRectMake(0, 0, 320, 480);//重要,设置Srollview控件的位置以及大小属性,

[m_Scroll setBackgroundColor:[UIColor blackColor]];

[m_Scroll setCanCancelContentTouches:NO];

m_Scroll.indicatorStyle = UIScrollViewIndicatorStyleWhite;

m_Scroll.clipsToBounds = YES; // default is NO, we want to restrict drawing within our scrollview

m_Scroll.scrollEnabled = YES;

m_Scroll.pagingEnabled = YES;

//m_Scroll.contentSize = CGSizeMake(4*320, 480);

UIView *view_1 =[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];

view_1.backgroundColor = [UIColor blueColor];

UIView *view_2 =[[UIView alloc] initWithFrame:CGRectMake(320, 0, 320, 480)];

view_2.backgroundColor = [UIColor yellowColor];

UIView *view_3 =[[UIView alloc] initWithFrame:CGRectMake(2*320, 0, 320, 480)];

view_3.backgroundColor = [UIColor grayColor];

UIView *view_4 =[[UIView alloc] initWithFrame:CGRectMake(3*320, 0, 320, 480)];

view_4.backgroundColor = [UIColor magentaColor];

[m_Scroll addSubview:view_1];

[m_Scroll addSubview:view_2];

[m_Scroll addSubview:view_3];

[m_Scroll addSubview:view_4];

[view_1 release];

[view_2 release];

[view_3 release];

[view_4 release];

/*

设置UIscrollview内容的大小,

这里需要注意的是第二个参数[m_Scroll bounds].size.height),这样设置就可以保证,不会在纵向因为子试图过大而滚动

我们在使用这个控件的时候,尽量设计好,UIScrollview控件的大小,以及起内容的大小,这样就不会出现不必要的滚动。

*/

[m_Scroll setContentSize:CGSizeMake((320 * 4), [m_Scroll bounds].size.height)];



2:根据内容的Frame属性,设置可视区域(visible area)

[scrollView scrollRectToVisible:CGRectMake(index*SceneWidth, 0, SceneWidth, SceneHeight) animated:YES];

后面的参数是可视区域的Frame属性,我们也可以这样理解,这个方法就是把UIScrollview的视图区域,移动到指定

Frame的可见区域。