关闭

UISlider_滑块

49人阅读 评论(0) 收藏 举报
分类:
/
//  SecondViewController.m
//  UISlider_滑块
//
//  Created by apple on 15/11/20.
//  Copyright © 2015 shuo. All rights reserved.
//

#import "SecondViewController.h"

@interface SecondViewController ()
@property(nonatomic,retain)UILabel *sliderValueLabel;//显示 value
@property(nonatomic,retain)UISlider *slider;//滚动条
@property(nonatomic,retain)UILabel *xLabel;//文字标签
@property(assign,nonatomic)CGFloat initialFontSize;//文字缩放尺寸

@end

@implementation SecondViewController

- (
void)viewDidLoad {
    [
super viewDidLoad];
   
   
self.view.backgroundColor =[UIColor whiteColor];
   
UIButton*button1 =[UIButton buttonWithType:UIButtonTypeCustom];
    button1.
frame=CGRectMake(100,570,180,30);
    [button1
setTitle:@"回到主页"forState:UIControlStateNormal];
    button1.
backgroundColor=[UIColor purpleColor];
    [
self.view addSubview:button1];
    [button1
addTarget:self action:@selector(buttonAction1:)forControlEvents:UIControlEventTouchUpInside];
   
//---------------------------------------------------------------
  
       
//显示 sliders 值的 label
   
self.sliderValueLabel =[[UILabel alloc]initWithFrame:CGRectMake(20, 30, 30, 30)];
   
self.sliderValueLabel.backgroundColor =[UIColor cyanColor];
   
self.sliderValueLabel.text =@"0";
    [
self.view addSubview:self.sliderValueLabel];
   
   
self.slider =[[UISlider alloc]initWithFrame:CGRectMake(100, 30, 200, 40)];
   
self.slider.minimumValue = 0;//最小值
   
self.slider.maximumValue = 25;//最大值
   
self.slider.thumbTintColor =[UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0];
   
    [
self.slider addTarget:self action:@selector(sliderValueChange) forControlEvents:UIControlEventValueChanged];
   
    [
self.view addSubview:self.slider];
   
//---------------------文字标签-------------------------------
   
   
self.xLabel =[[UILabel alloc]initWithFrame:CGRectMake(0, 100, 375, 400)];
   
self.xLabel.backgroundColor =[UIColor  cyanColor];
    self.xLabel.text =@"提莫";
    //--????????————————标签文字居中貌似被废---
     self.xLabel.textAlignment = UITextAlignmentCenter;
   
self.initialFontSize =self.xLabel.font.pointSize;
   
NSLog(@"initialFontSize  =%.2f",self.initialFontSize);
    [
self.view addSubview:self.xLabel];

   }
- (
void)sliderValueChange{
   
NSLog(@"sliderValueChange");
   
//更新sliderValueChange 的值
   
self.sliderValueLabel.text =[[NSString alloc]initWithFormat:@"%.0f",self.slider.value];
   
//缩放 xlabel 内容
   
CGFloat newFontSize =self.initialFontSize*(self.slider.value +1);
   
NSLog(@"newFontSize =%.2f",newFontSize);
   
   
self.xLabel.font = [self.xLabel.font fontWithSize:newFontSize];
   
   
   
   
   
}
    -(
void)dealloc{
       
        [
_sliderValueLabel release];
        [
_slider release];
        [_xLabel release];
        [super dealloc];
       
}


- (
void)buttonAction1:(UIButton*)btn{
  

    [
self dismissViewControllerAnimated:YES completion:^{
       
       
NSLog(@"回到主页");
    }];
   
   //控制标签的 value更新
   
UISlider *sli1 =(UISlider *)sli;
   self.label.text =[NSString stringWithFormat:@"%.2f",sli1.value];
   
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:402次
    • 积分:53
    • 等级:
    • 排名:千里之外
    • 原创:5篇
    • 转载:1篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档