/
// 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 ];
// 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 ];
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 ( @" 回到主页 " );
}];
}
- ( void )buttonAction1:( UIButton *)btn{
[ self dismissViewControllerAnimated : YES completion :^{
NSLog ( @" 回到主页 " );
}];
//
控制标签的
value
更新
UISlider *sli1 =( UISlider *)sli;
UISlider *sli1 =( UISlider *)sli;
self.label.text =[NSString stringWithFormat:@"%.2f",sli1.value];
}