1 - (void)viewDidLoad 2 { 3 [super viewDidLoad]; 4 [self.view setBackgroundColor:[UIColor grayColor]]; 5 // Do any additional setup after loading the view, typically from a nib. 6 MarqueeLabel *label = [[MarqueeLabel alloc]initWithFrame:CGRectMake(0, 0, 100, 20)]; 7 label.backgroundColor = [UIColor clearColor]; 8 9 float fadeLength = 15.0f; 10 CAGradientLayer* gradientMask = [CAGradientLayer layer]; 11 gradientMask.bounds = label.layer.bounds; 12 gradientMask.position = CGPointMake([label bounds].size.width / 2, [label bounds].size.height / 2); 13 NSObject *transparent = (NSObject*) [[UIColor clearColor] CGColor]; 14 NSObject *opaque = (NSObject*) [[UIColor blackColor] CGColor]; 15 gradientMask.startPoint = CGPointMake(0.0, CGRectGetMidY(label.frame)); 16 gradientMask.endPoint = CGPointMake(1.0, CGRectGetMidY(label.frame)); 17 float fadePoint = fadeLength/label.frame.size.width; 18 [gradientMask setColors: [NSArray arrayWithObjects: transparent, opaque, opaque, transparent, nil]]; 19 [gradientMask setLocations: [NSArray arrayWithObjects: 20 [NSNumber numberWithFloat: 0.0], 21 [NSNumber numberWithFloat: fadePoint], 22 [NSNumber numberWithFloat: 1 - fadePoint], 23 [NSNumber numberWithFloat: 1.0], 24 nil]]; 25 label.layer.mask = gradientMask; 26 27 label.text = @"者是一个测试程序测试长度适用的滚动的是就是"; 28 [self.view addSubview:label]; 29 }