美文网首页
轮播图和弹窗的制作

轮播图和弹窗的制作

作者: 小玉柚子 | 来源:发表于2016-07-13 23:22 被阅读0次

    //

    //  ViewController.m

    //轮播图

    //

    //  Created by lanou on 16/7/13.

    //  Copyright © 2016年lanou. All rights reserved.

    //

    #import"ViewController.h"

    @interfaceViewController()

    #define screenWidth [UIScreen mainScreen].bounds.size.width

    #define screenHeight [UIScreen mainScreen].bounds.size.height

    @property(nonatomic,strong)UIPageControl*pageControl;

    //滑动视图UIScrollView,自带了可滑动功能

    @property(nonatomic,strong)UIScrollView*scrollView;

    @end

    @implementationViewController

    - (void)viewDidLoad {

    [superviewDidLoad];

    //创建初始化滑动视图

    //    [UIScreen mainScreen].bounds包含了屏幕尺寸

    self.scrollView=[[UIScrollViewalloc]initWithFrame:[UIScreenmainScreen].bounds];

    for(NSIntegeri=0; i<6; i++) {

    //根据i循环创建UIImageView,再添加到滑动视图scrollView上面

    UIImageView*imageView=[[UIImageViewalloc]initWithFrame:CGRectMake(i*screenWidth,0,screenWidth,screenHeight)];

    NSString*imageName=nil;

    if(i==5) {

    imageName=@"1.jpg";

    }else{

    imageName=[NSStringstringWithFormat:@"%ld.jpg",i+1];

    }

    //加载响应的图片

    UIImage*image=[UIImageimageNamed:imageName];

    //设置图片

    imageView.image=image;

    //将imageView添加到滑动视图上

    [self.scrollViewaddSubview:imageView];

    }

    //添加滑动视图到屏幕上

    [self.viewaddSubview:self.scrollView];

    //设置滑动视图的滑动区域contentSize

    self.scrollView.contentSize=CGSizeMake(6*screenWidth,screenHeight);

    //整屏翻转

    self.scrollView.pagingEnabled=YES;

    //边界回弹

    self.scrollView.bounces=YES;

    //设置代理,代理是负责监听滑动视图整个滑动过程的

    self.scrollView.delegate=self;

    //开启一个定时器

    //    TimeInterval:时间间隔

    //每隔一定的时间隔,target会去执行selector这个方法

    //    [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(turnToNextImage) userInfo:(nil) repeats:YES];

    self.pageControl=[[UIPageControlalloc]initWithFrame:CGRectMake(80,500,150,20)];

    self.pageControl.numberOfPages=5;

    [self.viewaddSubview:self.pageControl];

    }

    //定时器触发的方法:跳转到下一张图片

    -(void)turnToNextImage

    {

    //先获取当前图片是第几张

    NSIntegerindex=self.scrollView.contentOffset.x/screenWidth;

    //跳转到下一张(设置偏移量)

    [self.scrollViewsetContentOffset:CGPointMake((index+1)*screenWidth,0)animated:YES];

    }

    //滑动视图滑动的时候调用

    - (void)scrollViewDidScroll:(UIScrollView*)scrollView

    {

    //    contentOffset是访问到了滑动视图的偏移量,包含了x和y轴的偏移量

    //    setContentOffset:animated:

    //    NSLog(@"offset.x=%f,offset.y=%f",scrollView.contentOffset.x,scrollView.contentOffset.y);

    }

    //scrollView结束减速(停止)

    - (void)scrollViewDidEndDecelerating:(UIScrollView*)scrollView

    {

    //判断是否为最后一张

    NSIntegerindex=scrollView.contentOffset.x/screenWidth;

    //    5表示最后一张图片,如果是最后一张图片就切换到第0张图片(设置偏移量为0,0)

    if(index==5) {

    [scrollViewsetContentOffset:CGPointMake(0,0)animated:YES];

    self.pageControl.currentPage=0;

    }else{

    self.pageControl.currentPage=index;

    }

    }

    //滑动结束的时候调用

    - (void)scrollViewDidEndScrollingAnimation:(UIScrollView*)scrollView

    {

    //先获取当前下标

    NSIntegerindex=scrollView.contentOffset.x/screenWidth;

    //是最后一张就设置偏移量为0,0

    if(index==5) {

    [scrollViewsetContentOffset:CGPointMake(0,0)animated:NO];

    self.pageControl.currentPage=0;

    }else{

    self.pageControl.currentPage=index;

    }

    }

    - (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

    }

    @end

    //

    //  ViewController.m

    //弹框

    //

    //  Created by lanou on 16/7/13.

    //  Copyright © 2016年lanou. All rights reserved.

    //

    //UITextfeild:怎么获取UITextFielld的文本,怎么设置输入框键盘类型,设置占位字符串

    #import"ViewController.h"

    @interfaceViewController()

    @end

    @implementationViewController

    - (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    }

    - (IBAction)alertAction:(id)sender {

    //弹框:UIAlertConteoller

    //    Title:大标题

    //    message:小标题

    //    preferredStyle:弹框样式

    UIAlertController*alertController=[UIAlertControlleralertControllerWithTitle:@"你有病"message:@"你有药啊"preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction*okAction=[UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction*_Nonnullaction) {

    //按钮按下要做的事情

    NSLog(@"淮南师范学院");

    }];

    UIAlertAction*cancelAction=[UIAlertActionactionWithTitle:@"取消"style:(UIAlertActionStyleCancel)handler:nil];

    //给弹框添加行为

    [alertControlleraddAction:okAction];

    [alertControlleraddAction:cancelAction];

    [selfpresentViewController:alertControlleranimated:YEScompletion:nil];

    }

    - (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

    }

    @end

    相关文章

      网友评论

          本文标题:轮播图和弹窗的制作

          本文链接:https://www.haomeiwen.com/subject/iolejttx.html