2017-04-20 44 views
1

我正在制作一个自定义UIProgressView,它最终会有绘制指示符,所以我自然覆盖了Draw方法。但是,我注意到在置换之后,即使调用Draw方法,用于递增和递减进度栏的方法也不再更新。UIProgressView不再更新绘制方法覆盖之后

自定义类:

public class ModifiedProgressBar : UIProgressView 
{ 
    public ModifiedProgressBar(IntPtr handle) : base(handle) 
    { 
    } 

    public override void Draw(CGRect rect) 
    { 
     base.Draw(rect); 
    } 
} 

调用者:

private void UpdateDisplay(float step) 
{ 
    this.modifiedProgressBar.SetProgress(step); 
} 

UpdateDisplay然后通过递增/递减按钮调用。此代码对UIProgressView班级以及ModifiedProgressBar班级的工作完全正常,但没有将Draw替代置于适当位置。 Progress属性也用新的设置值进行更新,视图只是不更新​​。我曾尝试致电SetNeedsDisplay,这并未强制视图更新。这里的封面发生了什么,有没有办法让它正确绘制?提前致谢!

回答

1

您实际上需要提供您的绘图例程,而不是调用基础,并且当然需要根据Progress属性更改绘图。

一些简单的黄色矩形:

enter image description here

public override void Draw(CGRect rect) 
{ 
    var color = UIColor.FromRGBA(1.0f, 1.0f, 0.0f, 1.000f); 
    var rectanglePath = UIBezierPath.FromRect(new CGRect(rect.X, rect.Y, rect.Width * Progress, rect.Height)); 
    color.SetFill(); 
    rectanglePath.Fill(); 
} 

或红色尖尖的thingie™:

enter image description here

public override void Draw(CGRect frame) 
{ 
    var context = UIGraphics.GetCurrentContext(); 
    var color2 = UIColor.FromRGBA(0.199f, 0.018f, 0.018f, 1.000f); 
    var shadow = new NSShadow(); 
    shadow.ShadowColor = UIColor.Black; 
    shadow.ShadowOffset = new CGSize(3.1f, 3.1f); 
    shadow.ShadowBlurRadius = 5.0f; 
    UIBezierPath bezierPath = new UIBezierPath(); 
    bezierPath.MoveTo(new CGPoint(frame.GetMinX() + 0.00935f * frame.Width * Progress, frame.GetMinY() + 0.01351f * frame.Height)); 
    bezierPath.AddLineTo(new CGPoint(frame.GetMinX() + 0.99537f * frame.Width * Progress, frame.GetMinY() + 0.50000f * frame.Height)); 
    bezierPath.AddLineTo(new CGPoint(frame.GetMinX() + 0.99537f * frame.Width * Progress, frame.GetMinY() + 0.50000f * frame.Height)); 
    bezierPath.AddLineTo(new CGPoint(frame.GetMinX() + 0.00935f * frame.Width * Progress, frame.GetMinY() + 0.98570f * frame.Height)); 
    bezierPath.AddLineTo(new CGPoint(frame.GetMinX() + 0.00935f * frame.Width * Progress, frame.GetMinY() + 0.01351f * frame.Height)); 
    bezierPath.ClosePath(); 
    bezierPath.LineCapStyle = CGLineCap.Square; 
    bezierPath.LineJoinStyle = CGLineJoin.Bevel; 
    context.SaveState(); 
    context.SetShadow(shadow.ShadowOffset, shadow.ShadowBlurRadius, shadow.ShadowColor.CGColor); 
    UIColor.Red.SetFill(); 
    bezierPath.Fill(); 
    context.RestoreState(); 
    color2.SetStroke(); 
    bezierPath.LineWidth = 1.0f; 
    bezierPath.Stroke(); 
} 
+0

感谢您的帮助,它看起来像这不是解决我的问题的最好方法。由于我不想丢失标准的'UIProgressView'的功能,我改写了自己的自定义类继承自'UIView',并将其绘制在应用程序中当前存在的'UIProgressView'上。您的详细答案帮助我了解当绘制方法被覆盖时在封面下发生了什么。 – PantAaroN