简体   繁体   English

iOS:动态更改UIView

[英]iOS : changing UIView Dynamically

I have an app that every time a user draws a freehand circle, the program does alloc/init to a circle class(uiview) which draws CAShapelayer circle instead the one was drawn by the user. 我有一个应用程序,每当用户绘制一个徒手绘制的圆圈时,该程序都会分配/初始化到一个绘制CAShapelayer圆圈的圆圈类(uiview),而不是由用户绘制。 now i want that when the user will press a button on the superview, the alpha of the drawn circle will change. 现在我希望当用户在超级视图上按下按钮时,绘制的圆的alpha将会改变。 this is the relevant code: 这是相关代码:

blueCircleView =[[ASFBlueCircle alloc]initWithFrame:CGRectMake(startPoint.x-10, startPoint.y-20, 60, 60)];

[self addSubview:blueCircleView];

and the init code from the class: 以及该类的初始化代码:

-(id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
    // Initialization code

    self.backgroundColor=[UIColor clearColor];
     UIBezierPath *circle = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 75, 75)];
   blueCircleLayer = [CAShapeLayer layer];
    blueCircleLayer.path = circle.CGPath;
    blueCircleLayer.strokeColor = [UIColor blueColor].CGColor;
    blueCircleLayer.fillColor = [UIColor clearColor].CGColor;
    blueCircleLayer.shadowColor =[UIColor blackColor].CGColor;
    blueCircleLayer.shadowOffset = CGSizeMake(0.0f, 5.0f);
    blueCircleLayer.shadowOpacity = 0.7f;
    blueCircleLayer.lineWidth = 7.0;

    [self.layer addSublayer:blueCircleLayer];

In ASFBlueCircle.h add property @property CAShapeLayer *blueCircleLayer; 在ASFBlueCircle.h中添加属性@property CAShapeLayer * blueCircleLayer;

In init, after addsublayer write a following line self.blueCircleLayer = blueCircleLayer; 在init中,在addsublayer之后编写以下行self.blueCircleLayer = blueCircleLayer;

And in the method called when button is pressed self.blueCircleLayer.color = [UIColor colorWithRed:red green:green blue:blue alpha:alpha].CGColor; 并且在按下按钮时调用的方法self.blueCircleLayer.color = [UIColor colorWithRed:red green:green blue:blue alpha:alpha] .CGColor;

with the red, green, blue, alpha values you want. 以及所需的红色,绿色,蓝色,alpha值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM