ios - IOS中的透明上下文?

标签 ios objective-c uiscrollview

我在 UIScrollView 中显示图像但我希望它与 UIImageView 融合在一起位于 UIScrollView 后面.

我目前正在尝试覆盖 - (void)drawRect:(CGRect)rectUIView 的子类中创建用于在 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/

相关文章:

iphone - 使 Storyboard 中的场景更长或在其中滚动

ios - 带有滑动和页面控制的图片幻灯片

c++ - 如何在更改编译器/操作系统方面使 C++ 代码健壮

objective-c - 将 NSString 转换为字符数组

ios - 如何正确更新AUSampler的loadInstrument属性?

ios - UIPageViewController 带有一张固定的背景图片

ios - 以编程方式使用按钮将 ViewController 推送到 UICollectionviewController Swift

ios - 快速将核心数据检索到 TableView 单元格中

objective-c - 更新 NSOutlineView 的内容

ios - 即使在编写 [activityIndi​​cator startAnimating] 之后,事件指示器也没有动画