标签: drawrect

动画绘制圆圈

我正在寻找一种动画绘制圆圈的方法.我已经能够创建圆圈,但它将所有这些组合在一起.

这是我的CircleView班级:

import UIKit

class CircleView: UIView {
  override init(frame: CGRect) {
    super.init(frame: frame)
    self.backgroundColor = UIColor.clearColor()
  }

  required init(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }


  override func drawRect(rect: CGRect) {
    // Get the Graphics Context
    var context = UIGraphicsGetCurrentContext();

    // Set the circle outerline-width
    CGContextSetLineWidth(context, 5.0);

    // Set the circle outerline-colour
    UIColor.redColor().set()

    // Create Circle
    CGContextAddArc(context, (frame.size.width)/2, frame.size.height/2, (frame.size.width - 10)/2, 0.0, CGFloat(M_PI * 2.0), 1)

    // Draw
    CGContextStrokePath(context);
  }
}
Run Code Online (Sandbox Code Playgroud)

以下是我如何将其添加到视图控制器中的视图层次结构中:

func addCircleView() …
Run Code Online (Sandbox Code Playgroud)

geometry progress drawrect uiviewanimation swift

98
推荐指数
5
解决办法
6万
查看次数

UIView有阴影,圆角和自定义drawRect

我必须创建一个UIView具有圆角,边框,阴影的自定义,并drawRect()重写其方法以提供自定义绘图代码,在该代码中将几条直线绘制到视图中(我需要使用快速,轻量级的方法,因为许多可以呈现这些视图).

我目前面临的问题是,一旦我drawRect()在视图类中重写(即使其中没​​有任何自定义代码),阴影也不再适用于圆角.请参阅附图中的差异:

在此输入图像描述

在视图控制器中我使用以下代码:

    view.layer.cornerRadius = 10;
    view.layer.masksToBounds = true;

    view.layer.borderColor = UIColor.grayColor().CGColor;
    view.layer.borderWidth = 0.5;

    view.layer.contentsScale = UIScreen.mainScreen().scale;
    view.layer.shadowColor = UIColor.blackColor().CGColor;
    view.layer.shadowOffset = CGSizeZero;
    view.layer.shadowRadius = 5.0;
    view.layer.shadowOpacity = 0.5;
    view.layer.masksToBounds = false;
    view.clipsToBounds = false;
Run Code Online (Sandbox Code Playgroud)

在被覆盖的drawContext()我会使用类似的东西:

    var context:CGContext = UIGraphicsGetCurrentContext();
    CGContextSetStrokeColorWithColor(context, UIColor.redColor().CGColor);
    // Draw them with a 2.0 stroke width so they are a bit more visible.
    CGContextSetLineWidth(context, 2.0);
    CGContextMoveToPoint(context, 0.0, 0.0); //start at this point
    CGContextAddLineToPoint(context, 20.0, 20.0); //draw to …
Run Code Online (Sandbox Code Playgroud)

objective-c uiview drawrect ios swift

50
推荐指数
5
解决办法
9万
查看次数

如何绘制UIImage或直接在-drawRect:?

我有一个UIImage我想画的UIView.但是我没有创建UIImageView并将其添加为子视图,而是想直接覆盖-drawRect:并绘制我的UIView.

例如,我的代码如下所示:

- (void)drawRect:(CGRect)rect {
    CGContextRef myContext = UIGraphicsGetCurrentContext();

    UIImage *img = [UIImage imageNamed:@"foo.png"];

    // how to draw the directly into the view now?
}
Run Code Online (Sandbox Code Playgroud)

iphone cocoa-touch uiimage drawrect

46
推荐指数
2
解决办法
5万
查看次数

如何在drawRect中填充渐变路径:?

用纯色填充路径很容易:

CGPoint aPoint;
for (id pointValue in points)
{
    aPoint = [pointValue CGPointValue];
    CGContextAddLineToPoint(context, aPoint.x, aPoint.y);
}
[[UIColor redColor] setFill];
[[UIColor blackColor] setStroke];
CGContextDrawPath(context, kCGPathFillStroke);
Run Code Online (Sandbox Code Playgroud)

我想绘制渐变而不是纯红色,但我遇到了麻烦.我已经尝试了问题/答案中列出的代码:iPhone上的UIView和UILabels上的渐变

这是:

CAGradientLayer *gradient = [CAGradientLayer layer];
[gradient setFrame:rect];
[gradient setColors:[NSArray arrayWithObjects:(id)[[UIColor blueColor] CGColor],
                (id)[[UIColor whiteColor] CGColor], nil]];
[[self layer] setMasksToBounds:YES];

[[self layer] insertSublayer:gradient atIndex:0];
Run Code Online (Sandbox Code Playgroud)

然而,这描绘了整个视图,它与渐变一起,掩盖了我的原始路径.

iphone core-graphics quartz-graphics drawrect

40
推荐指数
1
解决办法
4万
查看次数

drawRect在子视图之上

我有子类UIView并添加了一个drawRect方法.然后我使用此自定义类定义视图并向其添加子视图.

问题是drawRect似乎在子视图下绘制东西(因此不可见).

我希望drawRect绘制的内容出现在我的自定义子视图上方UIView.

这可能吗?

core-graphics z-order drawrect ios

38
推荐指数
2
解决办法
1万
查看次数

在iOS中绘制部分图像的最有效方法

给定a UIImage和a CGRect,绘制与CGRect(不缩放)对应的图像部分的最有效方式(在内存和时间上)是什么?

作为参考,这是我目前的做法:

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGRect frameRect = CGRectMake(frameOrigin.x + rect.origin.x, frameOrigin.y + rect.origin.y, rect.size.width, rect.size.height);    
    CGImageRef imageRef = CGImageCreateWithImageInRect(image_.CGImage, frameRect);
    CGContextTranslateCTM(context, 0, rect.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextDrawImage(context, rect, imageRef);
    CGImageRelease(imageRef);
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,对于中等大小的图像和高setNeedsDisplay频率,这似乎非常慢.使用UIImageView框架并clipToBounds产生更好的结果(灵活性较低).

iphone uiimage drawrect quartz-2d ios

36
推荐指数
2
解决办法
2万
查看次数

iOS绘制填充圆圈

这里不是图形程序员,所以我试图绊倒这个.我正在尝试绘制9个实心圆圈,每个圆圈都有不同的颜色,每个圆圈都有一个白色边框.UIView的框架是CGRectMake(0,0,60,60).见附图.

问题是我在每边的边界上都有"扁平斑点".以下是我的代码(来自UIView子类):

- (void)drawRect:(CGRect)rect
{
    CGRect borderRect = CGRectMake(0.0, 0.0, 60.0, 60.0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
    CGContextSetRGBFillColor(context, colorRed, colorGreen, colorBlue, 1.0);
    CGContextSetLineWidth(context, 2.0);
    CGContextFillEllipseInRect (context, borderRect);
    CGContextStrokeEllipseInRect(context, borderRect);
    CGContextFillPath(context);
}
Run Code Online (Sandbox Code Playgroud)

如果我在drawRect中更改为CGRectMake(0,0,56,56),我只会在顶部和左侧看到平点,并且底部和右侧看起来很好.

任何人都可以建议我如何解决这个问题?在我看来,UIView正在修剪边界,但对此并不了解,我真的不知道如何修复它.

在此之前,感谢任何图形专家的建议.

在此输入图像描述

cgcontext drawrect ios

34
推荐指数
4
解决办法
4万
查看次数

在Swift for iOS中沿圆形路径绘制文本

我正在寻找一些关于如何使用Swift2for 在圆形边缘周围绘制简单单线串的最新帮助/提示iOS9.我看到涉及旧ObjC片段的相当陈旧的例子,并且仅限于此OS X.这在自定义UIView子类的drawRect()方法中是否可以在iOS中实现?

drawrect ios swift swift2 swift3

32
推荐指数
3
解决办法
1万
查看次数

我怎样才能用渐变色调UIImage?

我到处搜索,但没有找到解决方案.我有图像1.如何以渐变方式对它们进行编程以获得图像2和3?以下是这些图片:

图像1,2,3

我通过Photoshop应用于它们的色调是简单的双色线性渐变.

我的问题是:如何以编程方式实现此效果?


解决方案: jrtc27给了我几乎成功的例子.我修复它(对于ARC)并使其可重用(使用UIImage的类别).就这个:

- (UIImage *)tintedWithLinearGradientColors:(NSArray *)colorsArr {
    CGFloat scale = self.scale;
    UIGraphicsBeginImageContext(CGSizeMake(self.size.width * scale, self.size.height * scale));
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(context, 0, self.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);

    CGContextSetBlendMode(context, kCGBlendModeNormal);
    CGRect rect = CGRectMake(0, 0, self.size.width * scale, self.size.height * scale);
    CGContextDrawImage(context, rect, self.CGImage);

    // Create gradient

    UIColor *colorOne = [colorsArr objectAtIndex:1]; // top color
    UIColor *colorTwo = [colorsArr objectAtIndex:0]; // bottom color


    NSArray *colors = [NSArray arrayWithObjects:(id)colorOne.CGColor, (id)colorTwo.CGColor, nil];
    CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
    CGGradientRef gradient …
Run Code Online (Sandbox Code Playgroud)

iphone core-graphics tint uiimage drawrect

29
推荐指数
4
解决办法
1万
查看次数

CGContextSaveGState与UIGraphicsPushContext

有两种drawRect方法:

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);
    // do drawing here
    CGContextRestoreGState(context);
}
Run Code Online (Sandbox Code Playgroud)

- (void)drawRect:(CGRect)rect 
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    UIGraphicsPushContext(context);
    // do drawing here
    UIGraphicsPopContext(); 
}
Run Code Online (Sandbox Code Playgroud)

UIGraphicsPushContext/UIGraphicsPopContext来自UIKit, 而CGContextSaveGState/CGContextRestoreGState来自CoreGraphics.

问题:这些方法有什么区别?哪一个更好用?是否有一些证明一种方法比其他方法更好的例子,反之亦然?

objective-c cgcontext drawrect ios

24
推荐指数
1
解决办法
6453
查看次数