我在 UIScrollView
中显示图像但我希望它与 UIImageView
融合在一起位于 UIScrollView
后面.
我目前正在尝试覆盖 - (void)drawRect:(CGRect)rect
在 UIView
的子类中创建用于在 ScrollView 中显示图像,使用方法drawInRect:blendMode:alpha:
或 CGContextSetBlendMode
但没有一个工作。我认为原因是当前上下文不透明,但我找不到解决方法。
最佳答案
您可以将 UIImageView 添加为 UIView 的子类( ScrollView 的父 View )。
您可以更改 UIImageView 的不透明度以匹配您的条件。如果您只想将图像作为 UIView 的背景,您可以设置 View 的背景并作为颜色,您可以使用 [UIColor colorWithPatternImage:Your UIImage here]
.
这是你需要的东西吗?
//扩展
我尝试了一下,下面的两个示例都将图像设置为背景,当我滚动图像时,图像不会移动:
示例 1:
[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"newtons_cradle.jpg"]]];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
[scrollView setContentSize:CGSizeMake(self.view.bounds.size.width, 1000.0f)];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 50, 300, 50)];
[label setBackgroundColor:[UIColor whiteColor]];
[label setText:@"Label Test Text"];
[scrollView addSubview:label];
[self.view addSubview:scrollView];
示例 2:
UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
[imageView setImage:[UIImage imageNamed:@"newtons_cradle.jpg"]];
[self.view addSubview:imageView];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
[scrollView setContentSize:CGSizeMake(self.view.bounds.size.width, 1000.0f)];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 50, 300, 50)];
[label setBackgroundColor:[UIColor whiteColor]];
[label setText:@"Label Test Text"];
[scrollView addSubview:label];
[self.view addSubview:scrollView]
;
关于ios - IOS中的透明上下文?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20402360/