2016-09-06 64 views
-1

嗨如何創建自定義UISlider,如下圖所示。如何創建自定義UISlider

enter image description here

我GOOGLE了很多還是沒能找到一個方法來做到這一點。任何人都可以建議我如何做到這一點。任何幫助將不勝感激。

回答

0

自定義UISlider

ViewController.h

#import <UIKit/UIKit.h> 

@interface ViewController :UIViewController 
{ 
} 

@property (strong, nonatomic) IBOutlet UILabel *LineLbl; 
@property (strong, nonatomic) IBOutlet UIScrollView *scrollviewObj; 

@end 

ViewController.m

#import "ViewController.h" 

@interface ViewController() 
{ 

} 

@end 

@implementation ViewController 
@synthesize LineLbl; 
@synthesize scrollviewObj; 


- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    UISlider *slider = [[UISlider alloc]initWithFrame:CGRectMake(10,LineLbl.frame.origin.y + LineLbl.frame.size.height +20, self.view.frame.size.width -20, 20)]; 
    slider.maximumValue = 1; 
    slider.value = 0.6; 
    [slider setMinimumTrackTintColor:[UIColor clearColor]]; 
    [slider setMaximumTrackTintColor:[UIColor clearColor]]; 
    [slider setThumbImage:[UIImage imageNamed:@"sliderImg"] forState:UIControlStateNormal]; 

    CAGradientLayer *red_gradient =[CAGradientLayer layer]; 

    UIColor *startColor=[UIColor yellowColor]; 
    UIColor *midColor=[UIColor colorWithRed:(155/255.f) green:(250/255.f) blue:(90/255.f) alpha:1]; 
    UIColor *endColor= [UIColor colorWithRed:(96/255.f) green:(255/255.f) blue:(59/255.f) alpha:1]; 

    red_gradient.frame = slider.bounds; 
    red_gradient.colors = [NSArray arrayWithObjects:(id)[startColor CGColor], (id)[midColor CGColor],(id)[endColor CGColor], nil]; 

    [red_gradient setStartPoint:CGPointMake(0.0, 0.5)]; 
    [red_gradient setEndPoint:CGPointMake(1.0, 0.5)]; 

    [slider.layer insertSublayer:red_gradient atIndex:2]; 

    [scrollviewObj addSubview: slider]; 
} 

我跟着this

+0

k謝謝讓我試試這個 –