簡體   English   中英

UIImageView IOS中間的透明圈

[英]Transparent Circle at Middle of UIImageView IOS

我需要在中間位置使UIImageView透明(圓形)。 在此處輸入圖片說明
我應該能夠通過該中間圈看到UIImageView的子視圖...我嘗試了CAShapeLayer ..但是我看不到UIImageView的子視圖..請幫助我..抱歉我的英語....

    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];

一種方法是這樣的。 我有一個包含主圖像的圖像視圖,還有另一個名為BlackCircle的圖像,它只是一個40x40的黑色圓圈(是我在Pixen應用中制作的)。 動作方法使主圖像在與黑圈重疊的地方變成透明,我將其放置在主圖像的中心:

@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)createAlphaClippedImage:(id)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;
}

與其嘗試使Imageview的一部分透明,不如通過將相應的alpha設置為0,使圖像在所需區域透明。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM