滑动视图和点的动态链接

//

//ViewController.m

//UIScrollView

//

#import"ViewController.h"

@interfaceViewController()

{

//定义全局变量

UIPageControl*pagectr;

}

@end

@implementationViewController

- (void)viewDidLoad {

[superviewDidLoad];

//设置滑动视图

UIScrollView* scrctr = [[UIScrollViewalloc]initWithFrame:CGRectMake(10,70,300,300)];

scrctr.backgroundColor= [UIColorgreenColor];

//决定能否滑动

scrctr.contentSize=CGSizeMake(300*3,300);

[self.viewaddSubview:scrctr];

UIImageView*vc1 = [[UIImageViewalloc]initWithFrame:CGRectMake(0,0,300,300)];

vc1.backgroundColor= [UIColorredColor];

[vc1setImage:[UIImageimageNamed:@"206ba6e062f9d4a9f60a23bc4e725a9b.jpg"]];

UIImageView*vc2 = [[UIImageViewalloc]initWithFrame:CGRectMake(300,0,300,300)];

[vc2setImage:[UIImageimageNamed:@"b742016c0ef87ff16d24318772c62eec.jpg"]];

vc2.backgroundColor= [UIColorblueColor];

UIImageView*vc3 = [[UIImageViewalloc]initWithFrame:CGRectMake(600,0,300,300)];

vc3.backgroundColor= [UIColorblackColor];

[vc3setImage:[UIImageimageNamed:@"c7bde3c3f51ee2f1377c4144241c0e6d.jpg"]];

[scrctraddSubview:vc1];

[scrctraddSubview:vc2];

[scrctraddSubview:vc3];

//隐藏水平滑动条

scrctr.showsHorizontalScrollIndicator=NO;

//取消回弹

scrctr.bounces=NO;

//分页效果

scrctr.pagingEnabled=YES;

//签订协议

scrctr.delegate=self;

//创建UIPageControl

pagectr= [[UIPageControlalloc]initWithFrame:CGRectMake(30,380,100,20)];

//设置背景颜色

pagectr.backgroundColor= [UIColorgrayColor];

//甚至点的个数

pagectr.numberOfPages=3;

//当前点的颜色

pagectr.currentPageIndicatorTintColor= [UIColorblueColor];

//没有选中点的颜色

pagectr.pageIndicatorTintColor= [UIColorwhiteColor];

//滑动事件

[pagectraddTarget:selfaction:@selector(scrollViewDidEndDecelerating:)forControlEvents:UIControlEventValueChanged];

[self.viewaddSubview:pagectr];

}

//停止减速,常用

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

NSLog(@"%f",scrollView.contentOffset.x);

pagectr.currentPage= scrollView.contentOffset.x/300;

}

@end


滑动视图和点的动态链接_第1张图片

你可能感兴趣的:(滑动视图和点的动态链接)