通过slider切换图片,缩放图片

@interface AstronomyViewController ()


{
    UIImageView * _backImageView;
    
    UIButton * _setButton;
    UIButton * _lookButton;
    
    UIView * _middleView;
    UILabel * _titleLabel;
    UIImageView * _imageView;
    UILabel * _textLabel;
    
    NSMutableArray * _arrayImage;//存放图片
    NSMutableArray * _arrayTitle;//存放名称
    NSMutableArray * _arrayText;//存放文本信息
    NSMutableArray * _arraySmall;//存放小图片


    UIView * _bottomView;//设置底端视图
    UISlider * _slider;//进度条
    
    UIView * _setView;//设置视图
    UISlider * _changeSlider;
    
    UIView * _lookView;
    
    UIImageView * _smallImageView;
    
    NSInteger _smallCurrent;//当前小视图位于第几个


}




@end


@implementation AstronomyViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor=[UIColor whiteColor];
    
    //提前隐藏夜间模式图片
    UIImage * image=[UIImage imageNamed:@"bg2.jpg"];
    _backImageView=[[UIImageView alloc]initWithFrame:CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height)];
    _backImageView.image=image;
    _backImageView.hidden=YES;
    [self.view addSubview:_backImageView];
    
    
    
    //加载数据
    [self _loadData];
    
    //‘设置’按钮
    [self _topSetButton];
    
    //查看按钮
     [self _topLookButton];
    
    //中间图片视图
    [self _middleImage];
    
    //加载进度条
    [self _bottomSlider];
    
}




#pragma mark - loadData
//加载数据
- (void)_loadData{
    
    //加载12张星座图片
    _arrayImage=[NSMutableArray arrayWithCapacity:12];
    for(int i=0;i<12;i++){
        NSString * imageName=[NSString stringWithFormat:@"star%d.jpg",i+1];
        UIImage * image=[UIImage imageNamed:imageName];
        [_arrayImage addObject:image];
    }
    
    //设置图片的题目名称
    _arrayTitle=[NSMutableArray arrayWithObjects:@"水瓶座",@"双鱼座",@"白羊座",@"金牛座",@"双子座",@"巨蟹座",@"狮子座",@"处女座",@"天秤座",@"天蝎座",@"射手座",@"摩羯座",nil];
    
    //设置文本信息内容
    NSString * text1=@"水瓶座的人,极富革新精神,具有独特的个性,有着超前的思想,是新思想的开拓者。聪颖脱俗,常有奇异的想法,不按牌理出牌。";
    NSString * text2=@"双鱼座的人,内向害羞,有着浓厚的艺术气息。柔情似水,浪漫多情,天真烂漫,会有许多梦幻般的想法,只是有时显得不切实际。";
    NSString * text3=@"白羊座的人乐观活泼、敢做敢当,干劲十足,属于剑及履及的行动派,是急行侠、探险家。但有时显得没有耐性,冲动易怒,太过冒险。";
    NSString * text4=@"金牛座的人给人的感觉稳重、务实,追求稳定与和谐,害怕变动,属于享受派。喜欢安定,最怕没有安全感。但有时显得固执己见,对钱财看得很重";
    NSString * text5=@"双子座的人,思维跳跃,口才伶俐,鬼点子多,常语出惊人,但有三心二意的倾向,情绪起伏多变,难以专心。";
    NSString * text6=@"巨蟹座的人,亲切有礼,感情丰富、细腻,有很强的感受力,具有母性的博爱之心,属于居家派的。但情绪起伏,有逃避倾向。";
    NSString * text7=@"狮子座的人,自信自强,气度非凡,极具领导统御能力,永不服输,是天生的贵族、王者。只是有时过度追求利,甚至有些自负,莽撞、以自我为中心。";
    NSString * text8=@"处女座的人,思虑周全,非常谦虚,谨慎保守,很有耐心,擅长分析,凡事要求做到尽善尽美,是个完美主义者。但易给人较大的压力,较被动严肃。";
    NSString * text9=@"天秤座的人很有气质,谈吐应对得体,善于与人沟通协调,是绝佳的谈判人才,属于迷人优雅派人物。只不过,思想易左右摇摆;追求外表光鲜,易失去自我。";
    NSString * text10=@"天蝎座的人,外表低调,实则企图心旺盛,充满神秘感,不会轻易地与人互动。对自我要求很高,极具竞争力与战斗力,有洞悉他人心思的能力。";
    NSString * text11=@"射手座的人,生性乐观开朗,热情奔放,崇尚自由,反应灵敏,极具创造力。待人友善又极具豪气,作风非常海派,人缘颇佳。";
    NSString * text12=@"摩羯座的人,很有自己的想法,很注重实际,耐力十足,意志坚定,有很强的时间观念,重视权威和名声,有不错的组织领导能力,是值得依赖的人。";:text1,text2,text3,text4,text5,text6,text7,text8,text9,text10,text11,text12,nil];
    
    
    _arraySmall=[NSMutableArray arrayWithCapacity:12];
    //添加图片
    for(int i=0;i<12;i++){
        NSString * imageName=[NSString stringWithFormat:@"icon%d.gif",i+1];
        UIImage * image=[UIImage imageNamed:imageName];
        [_arraySmall addObject:image];
        
    }
}




#pragma mark - setButton
    //设置最顶端的‘设置’按钮
- (void)_topSetButton{


    _setButton=[UIButton buttonWithType:UIButtonTypeCustom];
    _setButton.frame=CGRectMake(15,20,50,30);
    [_setButton setTitle:@"设置" forState:UIControlStateNormal];
    [_setButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    
    [_setButton addTarget:self action:@selector(clickSetButton) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_setButton];
    
    
    _setView=[[UIView alloc]initWithFrame:CGRectMake(0,self.view.frame.size.height,self.view.frame.size.width,180)];
    _setView.backgroundColor=[UIColor redColor];
    UIImage * image=[UIImage imageNamed:@"bg1.jpg"];
    UIImageView * imageView=[[UIImageView alloc]initWithFrame:_setView.bounds];
    imageView.image=image;
    [_setView addSubview:imageView];


    [self.view addSubview:_setView];
    
    
    //1>文字label
    UILabel * swtLabel=[[UILabel alloc]initWithFrame:CGRectMake(20,30,80,30)];
    swtLabel.Text=@"夜间模式";
    swtLabel.textColor=[UIColor whiteColor];
    swtLabel.textAlignment=NSTextAlignmentCenter;
    swtLabel.font=[UIFont systemFontOfSize:16];
    
    UILabel * sizeLabel=[[UILabel alloc]initWithFrame:CGRectMake(20,80,80,30)];
    sizeLabel.Text=@"图片尺寸";
    sizeLabel.textColor=[UIColor whiteColor];
    sizeLabel.textAlignment=NSTextAlignmentCenter;
    sizeLabel.font=[UIFont systemFontOfSize:16];
    
    
    //2>夜间模式切换开关
    UISwitch * swt=[[UISwitch alloc]initWithFrame:CGRectMake(100,30,50,45)];
    [swt addTarget:self action:@selector(changeBackGround:) forControlEvents:UIControlEventTouchUpInside];
    
    
    //3>图片尺寸放大缩小--进度条
    _changeSlider=[[UISlider alloc]initWithFrame:CGRectMake(100,60,200,80)];
    
    _changeSlider.value=1;
    _changeSlider.maximumValue=2;
    //添加事件,滑动进度条改变图片大小
    [_changeSlider addTarget:self action:@selector(changeImageSize) forControlEvents:UIControlEventValueChanged];
    
    
    [_setView addSubview:swtLabel];
    [_setView addSubview:sizeLabel];
    [_setView addSubview:swt];
    [_setView addSubview:_changeSlider];
    
}




#pragma mark - setView
//在设置视图上添加设置
//点击设置按钮
- (void)clickSetButton{
   
    //当再次点击时,恢复图片尺寸和slider的value值
    _changeSlider.value=1;
    _imageView.frame=CGRectMake(_middleView.frame.size.width/2.0-200/2.0,90,200,200);
 
    //设置视图出现时的动画
    CGRect rec=_setView.frame;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1];
    
    if(rec.origin.y==self.view.frame.size.height){
        
        rec.origin.y = self.view.frame.size.height-180;
    }else{
        rec.origin.y = self.view.frame.size.height;
    }
    _setView.frame=rec;
    [UIView commitAnimations];
    
    
//    _setView.hidden=!_setView.hidden;//显示设置视图
//    _bottomView.hidden=!_bottomView.hidden;//隐藏进度条视图
    
    [self.view bringSubviewToFront:_setView];//把设置视图放到最顶层
    
}




//改变视图背景为夜间模式---把隐藏的夜间模式图片显示出来
- (void)changeBackGround:(UISwitch *)swt{
    
    _backImageView.hidden=!_backImageView.hidden;


}


//改变图片大小---动画模式
- (void)changeImageSize{
    
    CGFloat value=_changeSlider.value;
    CGRect rec=_imageView.frame;
    
    rec.size.width = 200*value;
    rec.size.height = 200*value;
   
    [UIView beginAnimations:@"changeSize" context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:1];
    
    _imageView.bounds=rec;
    [UIView commitAnimations];


}




#pragma mark - middleImage
//中间图片视图---图片名称---文字
- (void)_middleImage{
    
    //1>设置中间部分的总体视图
    _middleView=[[UIView alloc]initWithFrame:CGRectMake(self.view.frame.size.width/2.0-300/2.0,70,300,450)];
    
    //2>往视图上添加东西
    
        //1>>添加标题名称--label
    _titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(_middleView.frame.size.width/2.0-100/2.0,20,100,30)];
    _titleLabel.Text=_arrayTitle[0];//初始化开始时的题目名称
    _titleLabel.textAlignment=NSTextAlignmentCenter;
    _titleLabel.font=[UIFont systemFontOfSize:22];
    
        //2>>添加星座图片
    _imageView=[[UIImageView alloc]init];
    _imageView.frame=CGRectMake(_middleView.frame.size.width/2.0-200/2.0,90,200,200);
    _imageView.image=_arrayImage[0];//初始化开始时的图片
    
        //3>>添加星座描述


    _textLabel=[[UILabel alloc]initWithFrame:CGRectMake(8,300,_middleView.frame.size.width-5,150)];
    _textLabel.Text=_arrayText[0];//初始化开始时的文本信息
    _textLabel.textAlignment=NSTextAlignmentLeft;
    _textLabel.numberOfLines=5;
    _textLabel.font=[UIFont systemFontOfSize:15];


    [_middleView addSubview:_titleLabel];
    [_middleView addSubview:_imageView];
    [_middleView addSubview:_textLabel];
    
    [self.view addSubview:_middleView];
}






#pragma mark - slider
//设置底端的进度条
- (void)_bottomSlider{
    
    //1>>设置一个视图
    _bottomView=[[UIView alloc]initWithFrame:CGRectMake(self.view.frame.size.width/2.0-300/2.0,530,300,100)];


    
    //2>>设置进度条
    _slider=[[UISlider alloc]initWithFrame:CGRectMake(_bottomView.frame.size.width/2.0-200/2.0,_bottomView.frame.size.height/2-80/2.0,200,80)];
    
    _slider.maximumValue=11;
    //添加事件,滑动进度条改变图片
    [_slider addTarget:self action:@selector(startChangeImage) forControlEvents:UIControlEventValueChanged];
    
    
    //3>>设置start end的label
    UILabel * startLabel=[[UILabel alloc]initWithFrame:CGRectMake(0,_bottomView.frame.size.height/2-30/2.0,50,30)];
    startLabel.Text=@"start";//初始化开始时的题目名称
    startLabel.textAlignment=NSTextAlignmentCenter;
    startLabel.font=[UIFont systemFontOfSize:20];
    
    UILabel * endLabel=[[UILabel alloc]initWithFrame:CGRectMake(_bottomView.frame.size.width-50,_bottomView.frame.size.height/2-30/2.0,50,30)];
    endLabel.Text=@"end";//初始化开始时的题目名称
    endLabel.textAlignment=NSTextAlignmentCenter;
    endLabel.font=[UIFont systemFontOfSize:20];


    [_bottomView addSubview:_slider];
    [_bottomView addSubview:startLabel];
    [_bottomView addSubview:endLabel];
    
    [self.view addSubview:_bottomView];
    
    
}




//滑动进度条,改变图片等信息
- (void)startChangeImage{
    
    int value=_slider.value;
    _titleLabel.Text=_arrayTitle[value];
    _imageView.image=_arrayImage[value];
    _textLabel.Text=_arrayText[value];
    _smallImageView.image= _arraySmall[value];
    
    _smallCurrent=_slider.value;


}






#pragma mark - lookSmallImage
- (void)_topLookButton{
    
    _lookButton=[UIButton buttonWithType:UIButtonTypeCustom];
    _lookButton.frame=CGRectMake(self.view.frame.size.width-65.0,20,50,30);
    [_lookButton setTitle:@"查看" forState:UIControlStateNormal];
    [_lookButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [_lookButton addTarget:self action:@selector(lookView) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:_lookButton];
    
    //查看视图
    _lookView=[[UIView alloc]initWithFrame:CGRectMake(0,self.view.frame.size.height,self.view.frame.size.width,180)];
    UIImage * image=[UIImage imageNamed:@"bg2.jpg"];
    UIImageView * imageView=[[UIImageView alloc]initWithFrame:_setView.bounds];
    imageView.image=image;
    
    [_lookView addSubview:imageView];
    [self.view addSubview:_lookView];


    [self lookSmallImage];
}


- (void)lookView{
    
    CGRect rec=_lookView.frame;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1];
    
    if(rec.origin.y==self.view.frame.size.height){
        
        rec.origin.y = self.view.frame.size.height-180;
    }else{
        rec.origin.y = self.view.frame.size.height;
    }
    _lookView.frame=rec;
    [UIView commitAnimations];
    
    [self.view bringSubviewToFront:_lookView];


}


- (void)lookSmallImage{
    
    //中间的小图片视图
    _smallImageView=[[UIImageView alloc]initWithFrame:CGRectMake(_lookView.frame.size.width/2.0-50,_lookView.frame.size.height/2.0-50,100,100)];
    _smallImageView.image=_arraySmall[_smallCurrent];
    [_lookView addSubview:_smallImageView];
    
    
      //左右切换按钮
    UIButton * leftBt=[UIButton buttonWithType:UIButtonTypeCustom];
    leftBt.frame=CGRectMake(_lookView.frame.size.width/2.0-100-20,_lookView.frame.size.height/2.0-45/2.0,50,45);
    [leftBt setBackgroundImage:[UIImage imageNamed:@"mvplayer_progress_reverse"] forState:UIControlStateNormal];
    [leftBt addTarget:self action:@selector(leftChange) forControlEvents:UIControlEventTouchUpInside];
    
    
    UIButton * rightBt=[UIButton buttonWithType:UIButtonTypeCustom];
    rightBt.frame=CGRectMake(_lookView.frame.size.width-_lookView.frame.size.width/2.0+100-50/2.0,_lookView.frame.size.height/2.0-45/2.0,50,45);
    [rightBt setBackgroundImage:[UIImage imageNamed:@"mvplayer_progress_forword"] forState:UIControlStateNormal];
    [rightBt addTarget:self action:@selector(rightChange) forControlEvents:UIControlEventTouchUpInside];
    
    [_lookView addSubview:leftBt];
    [_lookView addSubview:rightBt];
}


#pragma mark - 左右改变图片
//左右改变小视图和大的图片相关信息
- (void)leftChange{
    
    if(_smallCurrent==0){
        _smallCurrent=_arraySmall.count-1;
    }else{
        _smallCurrent--;
    }
    _titleLabel.Text=_arrayTitle[_smallCurrent];
    _imageView.image=_arrayImage[_smallCurrent];
    _textLabel.Text=_arrayText[_smallCurrent];
    _smallImageView.image= _arraySmall[_smallCurrent];
}


- (void)rightChange{
    
    if(_smallCurrent==11){
        _smallCurrent=0;
    }else{
        _smallCurrent++;
    }
    _titleLabel.Text=_arrayTitle[_smallCurrent];
    _imageView.image=_arrayImage[_smallCurrent];
    _textLabel.Text=_arrayText[_smallCurrent];
    _smallImageView.image= _arraySmall[_smallCurrent];


}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值