我想爲UITextView應用漸變效果。這是我的代碼:漸變效果UITextView
textView = [[UITextView alloc]initWithFrame:CGRectMake(50,220,self.view.frame.size.width-100,self.view.frame.size.height-300)];
underTextView = [[UIView alloc]initWithFrame:CGRectMake(50,220,self.view.frame.size.width-100,self.view.frame.size.height-300)];
CGFloat layerWidth = 8.f;
CAGradientLayer *gl1 = [CAGradientLayer layer];
id col1 = (id)[[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0]CGColor];
id col2 = (id)[[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0]CGColor];
gl1.colors = @[col2, col1, col1, col2];
if (self.textView.contentOffset.y < 5)
gl1.locations = @[@0.0, @0.0, @0.85, @1.0];
if (self.textView.contentOffset.y >=5 && self.textView.contentOffset.y < 20) {
CGFloat number = (self.textView.contentOffset.y - 5) * 0.01;
gl1.locations = @[@0.0, [NSNumber numberWithFloat:number], @0.85, @1.0];
}
if (self.textView.contentOffset.y >= 20 && self.textView.contentOffset.y < self.textView.contentSize.height - self.textView.frame.size.height - 20)
gl1.locations = @[@0.0, @0.15, @0.85, @1.0];
if (self.textView.contentOffset.y >= (self.textView.contentSize.height - self.textView.frame.size.height - 20) && self.textView.contentOffset.y < (self.textView.contentSize.height - self.textView.frame.size.height - 5)) {
CGFloat number = 1 - (self.textView.contentSize.height - self.textView.contentOffset.y - self.textView.frame.size.height - 5) * 0.01;
gl1.locations = @[@0.0, @0.15, [NSNumber numberWithFloat:number], @1.0];
}
if (self.textView.contentOffset.y >= self.textView.contentSize.height - self.textView.frame.size.height - 5)
gl1.locations = @[@0.0, @0.15, @1.0, @1.0];
gl1.frame = CGRectMake(0, 0, self.underTextView.frame.size.width - layerWidth, self.underTextView.frame.size.height);
CAGradientLayer *gl2 = [CAGradientLayer layer];
gl2.colors = @[col1, col1];
gl2.locations = @[@0.0, @1.0];
gl2.frame = CGRectMake(self.underTextView.frame.size.width - layerWidth, 0, layerWidth, self.underTextView.frame.size.height);
CAGradientLayer *gl = [CAGradientLayer layer];
[gl addSublayer:gl1];
[gl addSublayer:gl2];
self.underTextView.layer.mask = gl;
[self.underTextView addSubview:textView];
[self.view addSubview:underTextView];
但是漸變效果沒有被應用。有人可以提出錯誤的地方嗎?任何想法plsss。
謝謝,
我對textview有滾動效果,所以我想在textview和textview的底部應用漸變效果,同時滾動textview – asifa
啊,現在我明白了:) – arturdev