2019独角兽企业重金招聘Python工程师标准>>>
UIScrollView 可以滑动的视图
#import "ViewController.h"@interface ViewController (){UIImageView *imgV;}@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];//新加一个UIScrollViewUIScrollView *myScrollView = [[UIScrollView alloc]initWithFrame:self.view.frame];//改变UIScrollView的颜色myScrollView.backgroundColor = [UIColor redColor];//也可以将UIScrollView换成一张图片myScrollView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"444"]];//添加一个图片UIImage *img = [UIImage imageNamed:@"qidong.jpg"];imgV = [[UIImageView alloc]initWithImage:img];imgV.frame = CGRectMake(0, 0, img.size.width, img.size.height);//将图片添加到UIScrollView上[myScrollView addSubview:imgV];//将UIScrollView添加到手机界面上[self.view addSubview:myScrollView];//让UIScrollView可以交互(滑动)myScrollView.scrollEnabled = YES;//可以滑动的限制(设置这一步才可以滑动)//在这里只能上下滑动,不能左右滑动,因为可以滑动的宽度设置为屏幕的宽度,而高度是图片的高度[myScrollView setContentSize:CGSizeMake(self.view.frame.size.width, img.size.width)];//显示滑条[myScrollView showsHorizontalScrollIndicator];//设置减小速度myScrollView.decelerationRate = 0.1;//关闭滑条//关闭竖着的滑条myScrollView.showsVerticalScrollIndicator = NO;//关闭横着的滑条myScrollView.showsHorizontalScrollIndicator = NO;//设置滑条的位置myScrollView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 100, 100);//设置滑条的颜色myScrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;//图片的位置(可以设置下拉刷新用)myScrollView.contentInset = UIEdgeInsetsMake(100, 0, 0, 0);//锁定滑动的方向,只能向单一方向滑动,不能斜着跑偏myScrollView.directionalLockEnabled = YES;//不让自动跳到顶部(点上面的时候不能回去)myScrollView.scrollsToTop = NO;//滑动时候自动分页myScrollView.pagingEnabled = YES;//取消反弹myScrollView.bounces = YES;//设置UIScrollView的代理方法myScrollView.delegate = self;//放大缩小//最小可以缩小到1.0倍myScrollView.minimumZoomScale = 1.0;//最大可以放大到1.5倍myScrollView.maximumZoomScale = 1.5;//放大缩小后是不是反弹回来myScrollView.bouncesZoom = YES;}//引入UIScrollView的代理方法,注意在“.h”文件中要引入<UIScrollViewDelegate>
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{NSLog(@"已经滑动");}- (void)scrollViewDidZoom:(UIScrollView *)scrollView NS_AVAILABLE_IOS(3_2){NSLog(@"已经缩放");}- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{NSLog(@"将要开始拖拽");}- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset NS_AVAILABLE_IOS(5_0){NSLog(@"横着偏移%f 竖着偏移%f", velocity.x,targetContentOffset->y);NSLog(@"%f", targetContentOffset->y);//如果竖着偏移的距离为33,则滑动的图片下面的颜色改为黄色if((targetContentOffset->y = 33)){scrollView.backgroundColor = [UIColor yellowColor];}}- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{NSLog(@"结束拖拽");
}- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{NSLog(@"将要开始降速");}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{NSLog(@"结束减速");}- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{NSLog(@"结束滑动动画");}- (nullable UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{return imgV;}- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view NS_AVAILABLE_IOS(3_2){NSLog(@"开始缩放");}- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view atScale:(CGFloat)scale{NSLog(@"结束缩放 %f", scale);
}- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView{return YES;}- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView{NSLog(@"已经滑动到底部");}