【问题标题】:Create Square Rather Than Rounded Edge Effect UIProgressView创建方形而不是圆角边缘效果 UIProgressView
【发布时间】:2014-12-02 01:08:12
【问题描述】:

我有一个 UIProgressView,我试图让它具有方形而不是圆形边缘。我没有看到任何明显的方法可以做到这一点,所以我的想法是让进度图像本身比它的方形框架大一点,这样进度图像就会被裁剪成方形。但是,无论我如何改变进度图像的大小,我似乎都无法实现这种效果。谁能告诉我是否有办法实现我的目标?

这是我目前在 UIView 子类中的内容:

self.progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
self.progressView.trackImage = [PLStyle imageForColor:[PLColors greyLight] inFrame:CGRectMake(0, 0, 1, ProgressViewHeight)];
self.progressView.progressImage = [PLStyle imageForColor:[PLColors orange] inFrame:CGRectMake(0, 0, 1, ProgressViewHeight)];

[self.progressView setTranslatesAutoresizingMaskIntoConstraints:NO];
[self addSubview:self.progressView];

[self addConstraint:
 [NSLayoutConstraint constraintWithItem:self.progressView
                              attribute:NSLayoutAttributeLeading
                              relatedBy:NSLayoutRelationEqual
                                 toItem:self.imageView
                              attribute:NSLayoutAttributeTrailing
                             multiplier:1
                               constant:LeadingOrTrailingSpace]];

[self addConstraint:
 [NSLayoutConstraint constraintWithItem:self.progressView
                              attribute:NSLayoutAttributeCenterY
                              relatedBy:NSLayoutRelationEqual
                                 toItem:self
                              attribute:NSLayoutAttributeCenterY
                             multiplier:1
                               constant:0]];

[self addConstraint:
 [NSLayoutConstraint constraintWithItem:self.progressView
                              attribute:NSLayoutAttributeTrailing
                              relatedBy:NSLayoutRelationEqual
                                 toItem:self.counter
                              attribute:NSLayoutAttributeLeading
                             multiplier:1
                               constant:-LeadingOrTrailingSpace]];

[self addConstraint:
 [NSLayoutConstraint constraintWithItem:self.progressView
                              attribute:NSLayoutAttributeHeight
                              relatedBy:NSLayoutRelationEqual
                                 toItem:nil
                              attribute:NSLayoutAttributeNotAnAttribute
                             multiplier:1
                               constant:ProgressViewHeight]];

//上面用于在进度视图中创建图像的方法定义

+ (UIImage *)imageForColor:(UIColor *)color inFrame:(CGRect)rect
{
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

【问题讨论】:

    标签: ios objective-c xcode uiview uiprogressview


    【解决方案1】:

    UIProgressViewStyle 设置为bar

    目标-C:

    [progressView setProgressViewStyle: UIProgressViewStyleBar];
    

    斯威夫特:

    progressView.progressViewStyle = .bar
    

    【讨论】:

    • 谢谢。这是完美的答案。
    【解决方案2】:

    你总是可以很容易地自己滚动。

    @interface MyProgressView : UIView
    @property (assign) float progress;
    @end
    
    @implementation MyProgressView {
      UIView *progressBar;
    }
    - (void)setProgress:(float)progress {
      _progress = progress;
      [self setNeedsLayout];
    }
    - (void)layoutSubviews {
      CGRect frame = self.bounds;
      frame.size.width *= progress;
      progressBar.frame = frame;
    }
    @end
    

    如果更适合您的应用程序,您还可以使用“最小/最大值”+“设定值”并进行进度计算。如果您不喜欢将一堆UIViews 拼凑在一起进行简单的矩形绘制,也可以使用CALayers。

    【讨论】:

      【解决方案3】:

      使用progressViewStyle = .bar

       let p = UIProgressView(frame: .zero)
       p.progressViewStyle = .bar
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-01-21
        • 1970-01-01
        • 2019-07-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多