• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - UIImageView IOS中间的透明圆圈

[复制链接]
菜鸟教程小白 发表于 2022-12-13 05:30:50 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我需要使 UIImageView 在中间位置透明(圆环..见附图)enter image description here
我应该能够通过那个中间圆圈看到 UIImageView 的后面 subview ...我尝试了 CAShapeLayer ..但我看不到 UIImageView 的后面 subview ..请帮助我..对不起我的英语....

    CAShapeLayer *circleLayer = [CAShapeLayer layer];

    // Give the layer the same bounds as your image view
    [circleLayer setBounds:CGRectMake(100.0f, 100.0f, [((UIImageView *)view) bounds].size.width,
                                      [((UIImageView *)view) bounds].size.height)];
    // Position the circle anywhere you like, but this will center it
    // In the parent layer, which will be your image view's root layer
    [circleLayer setPosition:CGPointMake(100,
                                         100)];
    // Create a circle path.
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:
                          CGRectMake(100.0f, 100.0f, 50.0f, 50.0f)];
    // Set the path on the layer
    [circleLayer setPath:[path CGPath]];
    // Set the stroke color
    [circleLayer setStrokeColor:[[UIColor redColor] CGColor]];
    // Set the stroke line width
    [circleLayer setLineWidth:2.0f];

    // Add the sublayer to the image view's layer tree
    [circleLayer setOpacity:0.5];
[[((UIImageView *)view) layer] addSublayer:circleLayer];



Best Answer-推荐答案


一种方法是这样的。我的主图像有一个 ImageView ,还有一个名为 BlackCircle 的图像,它只是一个 40x40 的黑色圆圈(我在 Pixen 应用程序中制作的)。 action 方法使主图像在与黑色圆圈重叠的地方变为透明,我已将其放置在主图像的中心:

@interface ViewController ()
@property (weak,nonatomic) IBOutlet UIImageView *imageView;
@end

@implementation ViewController

-(void)viewDidLoad {
    [super viewDidLoad];
    self.imageView.image = [UIImage imageNamed"House.tiff"];
    self.imageView.backgroundColor = [UIColor clearColor];
}


-(IBAction)createAlphaClippedImageid)sender {
    UIImage *circleImage = [UIImage imageNamed"BlackCircle.png"];
    UIImage *mainImage = [UIImage imageNamed"House.tiff"];

    UIGraphicsBeginImageContextWithOptions(mainImage.size, NO, 0.0);

    CGRect imageRect = CGRectMake(0.0f, 0.0f, mainImage.size.width, mainImage.size.height);
    CGRect centerCircleRect = CGRectMake(mainImage.size.width/2.0 - circleImage.size.width/2.0, mainImage.size.height/2.0 - circleImage.size.height/2.0,circleImage.size.width,circleImage.size.height);

    [mainImage drawInRect:imageRect];
    [circleImage drawInRect:centerCircleRect blendMode:kCGBlendModeXOR alpha:1.0];

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    self.imageView.image = image;
}

关于ios - UIImageView IOS中间的透明圆圈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20064115/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap