【问题标题】:How to change size of thumb image of UISlider programmatically如何以编程方式更改 UISlider 拇指图像的大小
【发布时间】:2012-06-27 08:12:56
【问题描述】:

我想做自定义 UISlider, 像这样的

|o----------| -> |-----O------| -> |------------〇|

thumbImage在最小值时会很小,在滑块值增加的过程中会增加大小,否则会减小。

有人知道怎么做吗?

【问题讨论】:

    标签: ios objective-c uiimage uislider


    【解决方案1】:

    您可以使用此代码:

    + (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
        //UIGraphicsBeginImageContext(newSize);
        UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
        [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();    
        UIGraphicsEndImageContext();
        return newImage;
    }
    

    取自here

    您将要做的额外工作将是一个方法A,当UISlider's 值发生变化时,该方法将调用imageWithImage:scaledToSize:

    【讨论】:

    • 感谢回复,我已经添加了以下内容,对吗?但我测试图像大小出现问题...浮动比率 = penSize_sld.value/(penSize_sld.maximumValue/2); CGSize ss= CGSizeMake(penSize_sld.currentThumbImage.size.widthratio, penSize_sld.currentThumbImage.size.heightratio); UIImage *changeImage = [UIImage imageWithImage:penSize_sld.currentThumbImage scaledToSize:ss]; [penSize_sld setThumbImage:changeImage forState:UIControlStateNormal]; if (sender == penSize_sld) { BrushWidth = penSize_sld.value; }
    • 干得好!谢谢 JackyBoy float ratio = penSize_sld.value/( penSize_sld.maximumValue/2); if ( ratio < 0.8) { ratio = 0.8; } UIImage *thumbImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"drawview_dragbar_bu_1.png" ofType:nil]]; CGSize ss= CGSizeMake(thumbImage.size.width*ratio,thumbImage.size.height*ratio); UIImage *changeImage = [UIImage imageWithImage:thumbImage scaledToSize:ss]; [penSize_sld setThumbImage:changeImage forState:UIControlStateNormal]; ..... }
    【解决方案2】:

    斯威夫特 3:

    extension UIImage {
    
        func scaleToSize(newSize: CGSize) -> UIImage {
            UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0)
            draw(in: CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height))
            let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
            UIGraphicsEndImageContext();
            return newImage
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-01
      • 2015-07-17
      • 2017-06-16
      • 2012-10-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多