UIScrollViewDelegate 代理方法使用

//  ScrollViewViewController.m

//  ScrollView


#import"ScrollViewViewController.h"

#import"CustomA.h"

#import"CustomB.h"

@implementationScrollViewViewController

- (void)didReceiveMemoryWarning

{

// Releases the view if it doesn't have

a superview.

[superdidReceiveMemoryWarning];

// Release any cached data, images, etc

that aren't in use.

}

#pragma mark - View lifecycle

// Implement viewDidLoad to do additional setup after

loading the view, typically from a nib.

- (void)viewDidLoad

{

scrollview=[[UIScrollViewalloc]initWithFrame:CGRectMake(50,0,200,400)];

scrollview.contentSize=CGSizeMake(400,400);

scrollview.backgroundColor=[UIColororangeColor];

scrollview.pagingEnabled=YES;//是否自己动适应

viewA=[[CustomAalloc]initWithFrame:CGRectMake(50,0,100,400)];

viewA.backgroundColor=[UIColorblueColor];

[scrollviewaddSubview:viewA];

[viewA release];

CustomB* viewB=[[CustomBalloc]initWithFrame:CGRectMake(250,0,100,400)];

viewB.backgroundColor=[UIColoryellowColor];

[scrollview addSubview:viewB];

[viewB release];

[self.viewaddSubview:scrollview];

scrollview.maximumZoomScale=2.0;

scrollview.minimumZoomScale=0.5;

//    scrollview.decelerationRate=1;

scrollview.delegate=self;

//canCancelContentTouches:YES-移动手指足够长度触发滚动事件,NO-scrollView发生tracking

events后,就算用户移动手指,scrollView也不会滚动。

scrollview.canCancelContentTouches=NO;

//当值是YES的时候,用户触碰开始.要延迟一会,看看是否用户有意图滚动。假如滚动了,那么捕捉touch-down事件,否则就不捕捉。假如值是NO,当用户触碰,scrollview会立即触发

scrollview.delaysContentTouches=YES;

[scrollviewrelease];

[superviewDidLoad];

}

#pragma mark UIScrollViewDelegate

//只要滚动了就会触发

- (void)scrollViewDidScroll:(UIScrollView*)scrollView;

{

//    NSLog(@"scrollViewDidScroll");

NSLog(@"ContentOffset  x is %f,yis %f",scrollView.contentOffset.x,scrollView.contentOffset.y);

}

//开始拖拽视图

- (void)scrollViewWillBeginDragging:(UIScrollView*)scrollView;

{

NSLog(@"scrollViewWillBeginDragging");

}

//完成拖拽

- (void)scrollViewDidEndDragging:(UIScrollView*)scrollView willDecelerate:(BOOL)decelerate;

{

NSLog(@"scrollViewDidEndDragging");

}

//将开始降速时

- (void)scrollViewWillBeginDecelerating:(UIScrollView*)scrollView;

{

NSLog(@"scrollViewWillBeginDecelerating");

}

//减速停止了时执行,手触摸时执行

- (void)scrollViewDidEndDecelerating:(UIScrollView*)scrollView;

{

NSLog(@"scrollViewDidEndDecelerating");

}

//滚动动画停止时执行,代码改变时触发,也就是setContentOffset改变时

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView*)scrollView;

{

NSLog(@"scrollViewDidEndScrollingAnimation");

}

//设置放大缩小的视图,要是uiscrollview的subview

- (UIView*)viewForZoomingInScrollView:(UIScrollView*)scrollView;

{

NSLog(@"viewForZoomingInScrollView");

returnviewA;

}

//完成放大缩小时调用

- (void)scrollViewDidEndZooming:(UIScrollView*)scrollView withView:(UIView*)view atScale:(float)scale;

{

viewA.frame=CGRectMake(50,0,100,400);

NSLog(@"scale between minimum and maximum.

called after any 'bounce' animations");

}// scale between minimum and maximum. called after any

'bounce' animations

//如果你不是完全滚动到滚轴视图的顶部,你可以轻点状态栏,那个可视的滚轴视图会一直滚动到顶部,那是默认行为,你可以通过该方法返回NO来关闭它

- (BOOL)scrollViewShouldScrollToTop:(UIScrollView*)scrollView;

{

NSLog(@"scrollViewShouldScrollToTop");

returnYES;

}

- (void)scrollViewDidScrollToTop:(UIScrollView*)scrollView;

{

NSLog(@"scrollViewDidScrollToTop");

}

- (void)viewDidUnload

{

[superviewDidUnload];

// 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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容