相关疑难解决方法(0)

在2017年使用IBDesignable绘制虚线(不是虚线!)线

用UIKit 绘制虚线很容易.所以:

CGFloat dashes[] = {4, 2};
[path setLineDash:dashes count:2 phase:0];
[path stroke];
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

有没有办法画出真正的虚线?

在此输入图像描述

有任何想法吗?


由于这个问题很老,没有人提出完整的@IBDesignable解决方案,这里是...

希望能节省一些打字的人.

@IBDesignable class DottedVertical: UIView {

    @IBInspectable var dotColor: UIColor = UIColor.etc
    @IBInspectable var lowerHalfOnly: Bool = false

    override func draw(_ rect: CGRect) {

        // say you want 8 dots, with perfect fenceposting:
        let totalCount = 8 + 8 - 1
        let fullHeight = bounds.size.height
        let width = bounds.size.width
        let itemLength = fullHeight / CGFloat(totalCount)

        let path = UIBezierPath()

        let …
Run Code Online (Sandbox Code Playgroud)

uikit uiview ios swift

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

为什么CAShapeLayer的笔划延伸到框架之外?

这是示例代码:

//Called by VC:

HICircleView *circleView = [[HICircleView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];

// init of circle view

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        CAShapeLayer *borderLayer = [CAShapeLayer layer];
        borderLayer.fillColor = [UIColor whiteColor].CGColor;
        borderLayer.path = [UIBezierPath bezierPathWithOvalInRect:self.frame].CGPath;
        borderLayer.strokeColor = [[UIColor redColor] CGColor];
        borderLayer.lineWidth = 5;
        [self.layer addSublayer:borderLayer];
    }
    return self;
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

好的,谢谢你的回答.转移我:

CGRect rect = CGRectMake(3, 3, self.frame.size.width, self.frame.size.height);
borderLayer.path = [UIBezierPath bezierPathWithOvalInRect:rect].CGPath;
Run Code Online (Sandbox Code Playgroud)

并且制作了6个线宽.

core-animation objective-c ios

15
推荐指数
2
解决办法
9269
查看次数

在UIView动画期间,如何为UIView图层的子图层设置动画?

在视图的UIView动画中,您可以通过包含在options参数中来为其布局的子视图设置动画.然而,当它们不是某个视图的背衬层时,我无法找到一种方法来设置其子层的动画.他们只是跳到位以匹配视图的新界限.因为我正在使用图层而不是视图,所以我似乎必须使用Core Animation而不是UIView动画,但我不知道如何(以及何时)这样做,使得图层动画与视图匹配动画.UIViewAnimationOptionLayoutSubviews[UIView animateWithDuration:delay:options:animations:completion:]

这是我的基本问题.如果你想知道我想要完成的具体事情,请阅读更多内容.

我通过向视图的图层添加CAShapeLayer创建了一个带有虚线边框的视图(请参阅此stackoverflow问题:UIView周围的虚线边框).我调整CAShapeLayer的路径以匹配视图的边界layoutSubviews.

这是有效的,但有一个美容问题:当视图的边界在UIView动画中动画时(如旋转期间),虚线边框跳转到视图的新边界,而不是在视图动画其边界时平滑地动画.也就是说,当视图动画时,虚线边框的右边部分和底部部分不会分别保持与视图的右边部分和底部部分紧密相连.我怎样才能从CAShapeLayer中获取虚线边框,以便在视图动画边界时为视图设置动画?

到目前为止我正在做的是将CABasicAnimation附加到CAShapeLayer:

- (void)layoutSubviews
{
    [super layoutSubviews];

    self.borderLayer.path = [UIBezierPath bezierPathWithRect:self.bounds].CGPath;
    self.borderLayer.frame = self.bounds;

    CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
    [self.borderLayer addAnimation:pathAnimation forKey:nil];
}
Run Code Online (Sandbox Code Playgroud)

这确实会使虚线边框生成动画,但它没有正确的计时功能和动画持续时间来匹配视图动画.此外,有时候,我们不希望虚线边框有动画,例如,当视图首先进行布局时,边框不应该从一些旧路径动画到新的正确路径; 它应该出现.

calayer uiview cabasicanimation uiviewanimation ios

14
推荐指数
1
解决办法
4940
查看次数

在UIView子类中使用的CAShapeLayer不起作用

我尝试了几个小时用CAShapeLayer在我的UIView周围画一个虚线边框,但是我没有显示它.
ScaleOverlay.h

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>

@interface ScaleOverlay : UIView <UIGestureRecognizerDelegate> {
    CAShapeLayer *shapeLayer_;
}
@end
Run Code Online (Sandbox Code Playgroud)

ScaleOverlay.m

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
    self.backgroundColor = [UIColor redColor];
    self.alpha = 0;
    //Round corners
    [[self layer] setCornerRadius:8.f];
    [[self layer] setMasksToBounds:YES];
    //Border
    shapeLayer_ = [[CAShapeLayer layer] retain];

    CGMutablePathRef path = CGPathCreateMutable();
    CGPathAddRect(path, NULL, frame);
    shapeLayer_.path = path;
    CGPathRelease(path);
    shapeLayer_.backgroundColor = [[UIColor clearColor] CGColor];
    shapeLayer_.frame = frame;
    shapeLayer_.position = self.center;

    [shapeLayer_ setValue:[NSNumber numberWithBool:NO] forKey:@"isCircle"];
    shapeLayer_.fillColor = [[UIColor blueColor] CGColor];
    shapeLayer_.strokeColor …
Run Code Online (Sandbox Code Playgroud)

iphone core-animation ios

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

UITextView/UITextField的虚线/虚线边框

我想为我和我设置一个虚线/虚线边框.UITextFieldUITextView

我怎样才能做到这一点?我知道,我可以设置这行代码的边界:

[self.textFieldCardTitle.layer setBorderWidth:1.0];
[self.textFieldCardTitle.layer setBorderColor:[[UIColor whiteColor] CGColor]];  
Run Code Online (Sandbox Code Playgroud)

注意:我已经有了添加UIImageView背后的想法,UITextView并设置了带有虚线边框的图像.但我不想那样解决它.

border objective-c uitextfield uitextview ios

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