一.轮播图(自己写的,需要手动点击,可作引导页)
完整实例代码:
@interface NewfeatureViewController ()<UIScrollViewDelegate>
//声明一个UIScrollView的属性
@property(nonatomic,strong)UIScrollView *scrollView;
//声明一个可变数组
@property(nonatomic,strong)NSMutableArray *imageArray;
//声明一个UIPageControl 的属性
@property(nonatomic,strong)UIPageControl *pageControl;
@end
/*
轮播图的实现
*/
@implementation NewfeatureViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
/**这里的执行顺序不能乱 */
[self loadScrollView];
[self loadImageView];
[self loadPageControl];
}
-(void)loadScrollView
{
//初始化一个UIScrollView
self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
//是否整屏翻滚
self.scrollView.pagingEnabled = YES;
//设置边框是否回弹
self.scrollView.bounces = NO;
//设置背景色
self.scrollView.backgroundColor = [UIColor orangeColor];
//设置代理人
self.scrollView.delegate = self;
//添加在view上
[self.view addSubview:self.scrollView];
}
-(void)loadImageView
{
self.imageArray = [NSMutableArray array];
for (int i = 0; i<4; i++) {
//取出图片名字放假呢imageStr中
NSString *imageStr =[NSString stringWithFormat:@"new_feature_%d",i+1];
//根据imageStr , 初始化一个UIImage类型的对象
UIImage *myImage = [UIImage imageNamed:imageStr];
//定义UIImageView 通过循环变量控制图片位置
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i*self.view.frame.size.width, 0,
self.view.frame.size.width, self.view.frame.size.height)];
imageView.image = myImage;
//把imageview 添加进scrollview中进行显示 subView是一个数组存的是view所有的子视图
[self.scrollView addSubview:imageView];
//把图片添加进定义的可变数组中
[self.imageArray addObject:imageView];
//如果是最后一个imageView 就往里面添加其他内容
if (self.imageArray.count == 4) {
[self setupLastImageView:imageView];
}
}
//要在确保有数据之后 在设置contentSize
self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * self.imageArray.count,
self.view.frame.size.height);
}
-(void)loadPageControl
{
//UIPageControl 的初始化
self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 40,
self.view.frame.size.width, 40)];
//指定页面的个数
self.pageControl.numberOfPages = 4;
//指定当前页
self.pageControl.currentPage = 0;
[self.view addSubview:self.pageControl];
//设置背景色
self.pageControl.backgroundColor =[[UIColor blackColor]colorWithAlphaComponent:0.3];
//UIPageControl 的addTarget/action事件
[self.pageControl addTarget:self action:@selector(pageAction) forControlEvents:UIControlEventTouchUpInside];
}
-(void)pageAction
{
//通过pageControl 当前页乘以宽度 计算出当前的偏移量,从而通过偏移量的变化是实现相片的变化
[self.scrollView setContentOffset:CGPointMake(self.pageControl.currentPage * self.view.frame.size.width, 0)
animated:YES];
//打印偏移量的方法 (注意上面的动画效果对于偏移量造成一定的延迟,偏移量是在动画效果结束之后才最终确定下来,而打印事件是在setPoi
ntoffset(设置偏移量)时就打印的)
NSLog(@"pagecontrol = %@",NSStringFromCGPoint(self.scrollView.contentOffset));
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
//利用偏移量计算当前是第几张图片
NSInteger index = self.scrollView.contentOffset.x/self.view.frame.size.width;
//然后将计算出的第几张图片的下标赋给cuuentPage,从而实现图片切换
self.pageControl.currentPage = index;
//打印偏移量的方法
NSLog(@"%@",NSStringFromCGPoint(self.scrollView.contentOffset));
}
-(void)setupLastImageView:(UIImageView *)imageView
{
//开启用户交互功能
imageView.userInteractionEnabled = YES;
//1.分享给大家
UIButton *shareBtn = [[UIButton alloc]initWithFrame:CGRectMake(90, 400, 200, 40)];
//设置图片
[shareBtn setImage:[UIImage imageNamed:@"new_feature_share_false"] forState:UIControlStateNormal];
[shareBtn setImage:[UIImage imageNamed:@"new_feature_share_true"]
forState:UIControlStateSelected];
//设置文字
[shareBtn setTitle:@"分享给大家" forState:UIControlStateNormal];
[shareBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
shareBtn.titleLabel.font = [UIFont systemFontOfSize:16];
[shareBtn addTarget:self action:@selector(shareClick:) forControlEvents:UIControlEventTouchUpInside];
//调整内边距
shareBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
[imageView addSubview:shareBtn];
//2.开始微博
UIButton *startBtn = [[UIButton alloc] initWithFrame:CGRectMake(115, 480, 150, 40)];
[startBtn setBackgroundImage:[UIImage imageNamed:@"new_feature_finish_button"] forState:UIControlStateNormal];
[startBtn setBackgroundImage:[UIImage imageNamed:@"new_feature_finish_button_highlighted"]
forState:UIControlStateSelected];
[startBtn setTitle:@"开始微博" forState:UIControlStateNormal];
[startBtn addTarget:self action:@selector(startClick) forControlEvents:UIControlEventTouchUpInside];
[imageView addSubview:startBtn];
}
-(void)shareClick:(UIButton *)shareBtn
{
//状态取反
shareBtn.selected = !shareBtn.isSelected;
}
//开始微博按钮的设置
-(void)startClick
{
//切换到TabBarVieewController
/*
切换控制器的方法
1.push 依赖于UInavigationController 控制器的切换是可逆的
2.modal 模态推出
2.切换window的rootViewController
*/
//这是模态推出视图的方法
// TabBarViewController *main = [[TabBarViewController alloc] init];
// [self presentViewController:main animated:YES completion:nil];
UIWindow *window = [UIApplication sharedApplication].keyWindow;
window.rootViewController = [[TabBarViewController alloc] init];
}