iphone - 动画 CGContextFillRect

标签 iphone objective-c ios

我有一个进度 View ,我正在尝试为正在进行的更改设置动画。你打算怎样做呢?下面是绘制进度的代码。

提前致谢

- (void)drawRect:(CGRect)rect {
    // Drawing code.
    CGContextRef context = UIGraphicsGetCurrentContext();

    // draw the background
    CGContextSaveGState(context);
    UIBezierPath *outerPath = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:0];
    [outerPath addClip];

    CGPoint backgroundStartPoint = CGPointMake(0.0f, 0.0f);
    CGPoint backgroundEndPoint = CGPointMake(0.0f, CGRectGetHeight(rect));
    CGContextDrawLinearGradient(context, _backgroundGradient, backgroundStartPoint, backgroundEndPoint, 0);

    CGContextRestoreGState(context);

    // draw the progress
    CGContextSaveGState(context);
    UIBezierPath *innerPath = [UIBezierPath bezierPathWithRoundedRect:CGRectInset(rect, 1.0f, 1.0f) cornerRadius: 0];
    [innerPath addClip];

    [_glossTintColor setFill];
    CGRect progressRect = CGRectMake(0, 0, CGRectGetWidth(rect)*_progress, CGRectGetHeight(rect));
    CGContextFillRect(context, progressRect);

    CGContextRestoreGState(context);
}

最佳答案

每当进度发生变化时,使用[view setNeedsDisplay];触发重绘

请参阅UIView referencesetNeedsDisplay上。

例如,假设您创建的自定义 View 有一个名为 progress 的属性,实例变量名为 _progress。我们还假设属性由某个事件/计时器/附加线程更新。

- (void)setProgress:(float)progress
{
    _progress = progress;
    [self setNeedsDisplay];
}

这会覆盖原始 setter 并添加上述功能。

关于iphone - 动画 CGContextFillRect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10961037/

相关文章:

ios - 无法创建 IOS 分发 Cirtificates itunes 连接

iphone - 应用程序启动期间 iOS 测试与服务器的连接时间过长

iphone - 循环中的 Free 和 Malloc - 释放内存的正确方法?

objective-c - 自检 OS X 代码签名

ios - 停止和启动 MonitoringForRegion 时监控区域失败 - iOS 7.1

ios - 如何关闭当前 View Controller 并快速输入一个没有箭头的新 View Controller ?

iphone - 核心数据 : sorting before saving

iphone - 产生两个数字的每个组合,例如 1000

objective-c - 带有 Objective-C 的 MVC。 View 的格式化数据发生在哪里?

iphone - 如何检测按下的返回键并使用 UIKeyInput 协议(protocol)对其进行响应?