回答
有幾種方法可以做這樣的黑暗漸變。最簡單的一種是使用一個CAGradientLayer
像這樣
// Create a gradient layer
CAGradientLayer *layer = [CAGradientLayer layer];
// gradient from transparent to black
layer.colors = @[(id)[UIColor clearColor].CGColor, (id)[UIColor blackColor].CGColor];
// set frame to whatever values you like (not hard coded like here of course)
layer.frame = CGRectMake(0.0f, 0.0f, 320.0f, 200.0);
// add the gradient layer as a sublayer of you image view
[imageView.layer addSublayer:layer];
另一種選擇是,以產生具有伸縮性UIImage
,並用它在其中被添加作爲一個子視圖到原始UIImageView
一個UIImageView
,像這樣:
CGFloat gradientHeight = 200.0f; // set to whatever height you want your gradient to be
CGSize imageSize = CGSizeMake(1.0f, gradientHeight); // image will be stretched so can be 1pt wide
// prepare image with gradient
UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0.0f);
CGContextRef context = UIGraphicsGetCurrentContext();
// gradient from transparent to black
NSArray *colors = @[(id)[UIColor clearColor].CGColor, (id)[UIColor blackColor].CGColor];
CGGradientRef gradient = CGGradientCreateWithColors(CGColorSpaceCreateDeviceRGB(), (CFArrayRef)colors, NULL);
CGContextDrawLinearGradient(context, gradient, CGPointZero, CGPointMake(0.0f, imageSize.height), kCGGradientDrawsAfterEndLocation);
CGGradientRelease(gradient);
// get image from the context
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// create image view with the gradient image
UIImageView *gradientImageView = [[UIImageView alloc] initWithImage:image];
// set frame height to same value as imageHeight, and width to fill the superview (ignore this hard-coded 320)
gradientImageView.frame = CGRectMake(0.0f, 0.0f, 320.0f, gradientHeight);
// add the gradient image view as a subview to the image view
[imageView addSubview:gradientImageView];
這兩個選項都會給你這樣的結果:
我試過了這兩個選項,它們在普通視圖控制器中運行良好。但是,如果我使用表格視圖單元格(每個單元格都有圖像視圖),並且當我滾動表格視圖時,漸變圖層會朝着圖像頂部逐漸增加,然後完整圖像以黑色顯示。 – Javeed 2014-11-25 07:57:22
@只要細胞被重用(如果我理解正確),你就是probaby創建並再次添加它。您應該在創建時將漸變添加到單元格中,並且只在重新使用單元格時調整其框架。 – 2014-11-25 07:58:59
你是對的。我在「cellForRowAtIndexPath」方法中使用了上面的代碼片段。現在我已將代碼放在自定義單元格「awakeFromNib」方法中,並按照我的要求工作。 – Javeed 2014-11-25 09:29:10
您可以通過創建自定義視圖來完成此操作。
- 首先添加圖片查看內容。
- 爲文本添加一個半透明容器。
- 將標籤添加到半透明容器。
- 實施layoutSubviews進行佈局。
實施看起來是這樣的:
@implementation MyCustomView
{
UIImageView _imageView;
UIView _labelConainer;
}
//If this is a table view use initWithStyle:reuseIdentifier instead
- (instancetype)initWithFrame:(CGRect)frame {
[self setupImageView];
[self setupLabels];
}
- (void)layouSubViews {
[_imageView setFrame:self.bounds];
[_labelContainer setFrame:CGRectMake(
0, self.frame.size.height - 50, self.frame.size.width, 50)
//Layout labels in semi-transparent container, relative to parent.
}
- (void)setupImageView {
_imageView = [UIImageView alloc] initWithImage:anImage];
//For table views instead use self.contentView
[self addSubview:_iamgeView];
}
- (void)setupLabelContainer {
_labelContainer = [[UIView alloc] init];
[_labelContainer setBackgroundColor:[UIColor blackColor]];
[_labelContainer setAlpha:0.7];
[self addSubview:_labelContainer];
//Add some labels to label container
}
你也可以在一個XIB此設置,並使用自動佈局。
- 1. 如何在android中的圖像上放置黑色透明
- 2. 在白色和黑色的圖像上使用透明,使透明度倒置
- 3. 使黑色像素透明
- 4. jcrop變成透明圖像黑色
- 5. 在透明圖像上放置透明圖像
- 6. 在Android中放置半透明圖像
- 7. Android透明黑色圖層?
- 8. 將圖像放在另一個使黑色部件透明的位置
- 9. 爲什麼透明像素在圖像中顯示爲黑色?
- 10. 將透明圖像放置到另一個不透明圖像
- 11. 透明PNG黑色
- 12. 如何讓背景圖像的黑色部分透明?
- 13. OpenGL透明圖像在其中有黑色
- 14. 如何在CSS中的圖像上放置不透明的白色漸變?
- 15. 透明圖像在緩存位圖上顯示爲黑色
- 16. 像素每像素圖像,透明變成黑色
- 17. 如何將帶有透明背景的白色徽標圖像放在黑色標題中?
- 18. UIBarButtonItem - 如何製作半透明黑色?
- 19. 構建輪流部分透明的圖像像素黑色
- 20. 包含Texture Packer的透明像素圖像爲黑色
- 21. 如何使用ImageMagick着色黑色透明PNG圖標
- 22. 黑色背景快到了透明的圖像,同時在PHP
- 23. 在IE7-8中褪色透明圖像
- 24. 透明位圖是黑色的
- 25. 位圖轉換 - 從B&W源創建透明+黑色圖像
- 26. 圖像上的黑色透明覆蓋圖
- 27. iOS着色透明圖像
- 28. 如何在img上放置文本,並使圖像透明
- 29. 如何在圖像上放置透明按鈕?
- 30. iOS 5黑色透明UIAlertView
我不是倒向選民,而是試着對你的問題更具體。不要說「先謝謝你的建議」,問「我該怎麼去做這件事?」。 。並解釋你目前理解的深度,比如你到目前爲止所嘗試的內容。 – 2014-11-25 07:27:06