ios UISlider 的简单使用

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    
    UISlider *pSlider=[[UISlider alloc]initWithFrame:CGRectMake(60, 100, 200, 10)];
    [self.window addSubview:pSlider];
    
    //设置tag
    pSlider.tag=100;
    //先设置最大最小值
    pSlider.maximumValue=1;
    pSlider.minimumValue=0;
    //在设置初始值
//    pSlider.value=0.5;
    //添加方法 当值改变时
    [pSlider addTarget:self action:@selector(changeValue:) forControlEvents:UIControlEventValueChanged];
    //在里面drag
    [pSlider addTarget:self action:@selector(changeValue1:) forControlEvents:UIControlEventTouchDragInside];
    //在外drag
    [pSlider addTarget:self action:@selector(changeValue2:) forControlEvents:UIControlEventTouchDragOutside];
    //压栈方法 每一秒
//    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(SliderMethod:) userInfo:pSlider repeats:YES];
    
    [pSlider release];
    
    
    [self.window makeKeyAndVisible];
    return YES;
}

-(void)changeValue:(UISlider *)slider
{
    NSLog(@"当前值:%.2f",slider.value);
}

-(void)changeValue1:(UISlider *)slider
{
    NSLog(@"UIControlEventTouchDragInside ");
}

-(void)changeValue2:(UISlider *)slider
{
    NSLog(@"UIControlEventTouchDragOutside ");
}
-(void)SliderMethod:(NSTimer *)timer
{
    UISlider *slider=[timer userInfo];
    float value=slider.value;
    if (value<10) {
        value +=0.1;
        slider.value=value;
    }
}

你可能感兴趣的:(ios UISlider 的简单使用)