gpt4 book ai didi

ios - Swift - UISlider 的自定义控件

转载 作者:搜寻专家 更新时间:2023-11-01 05:41:10 24 4
gpt4 key购买 nike

我已经为 UISlider 创建了一个自定义控件。这个 UISlider 有 2 个拇指,所以它可以找到最小值和最大值。我将这些教程作为我的引用。

  1. http://www.raywenderlich.com/76433/how-to-make-a-custom-control-swift
  2. http://www.sitepoint.com/wicked-ios-range-slider-part-one/

编译成功但我的 UISlider 看起来很奇怪。您可以在此处查看我的 github 存储库。

https://github.com/datomnurdin/RevivalxSwiftSlider

我的 UISlider 自定义控件

import UIKit

class RangeSlider: UIControl {

var minimumValue: Float!
var maximumValue: Float!
var minimumRange: Float!
var selectedMinimumValue: Float!
var selectedMaximumValue: Float!

var distanceFromCenter: Float!

var _padding: Float!

var _maxThumbOn: Bool!
var _minThumbOn: Bool!

var _minThumb = UIImageView()
var _maxThumb = UIImageView()
var _track = UIImageView()
var _trackBackground = UIImageView()

override init(frame: CGRect){
super.init(frame: frame)

_minThumbOn = false;
_maxThumbOn = false;
_padding = 20;

let _trackBackground = UIImageView(image: UIImage(named: "bar-background.png"))
_trackBackground.center = self.center;
self.addSubview(_trackBackground)

let _track = UIImageView(image: UIImage(named: "bar-highlight.png"))
_track.center = self.center
self.addSubview(_track)


let _minThumb = UIImageView(image: UIImage(named: "handle.png"), highlightedImage: UIImage(named: "handle-hover.png"))
_minThumb.frame = CGRectMake(0,0, self.frame.size.height,self.frame.size.height);
_minThumb.contentMode = UIViewContentMode.Center
self.addSubview(_minThumb)

let _maxThumb = UIImageView(image: UIImage(named: "handle.png"), highlightedImage: UIImage(named: "handle-hover.png"))
_maxThumb.frame = CGRectMake(0,0, self.frame.size.height,self.frame.size.height)
_maxThumb.contentMode = UIViewContentMode.Center
self.addSubview(_maxThumb)
}

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


override func layoutSubviews() {


_minThumb.center = CGPointMake(CGFloat(self.xForValue(selectedMinimumValue)), self.center.y);

_maxThumb.center = CGPointMake(CGFloat(self.xForValue(selectedMaximumValue)), self.center.y)

NSLog("Tapable size %f", _minThumb.bounds.size.width);
self.updateTrackHighlight()

}

func xForValue(value: Float) -> Float {
return (Float(self.frame.size.width) - (_padding * 2)) * ((value - minimumValue) / (maximumValue - minimumValue)) + _padding
}

func valueForX(x: Float) -> Float {
return minimumValue + (x - _padding) / (Float(self.frame.size.width) - (_padding * 2)) * (maximumValue - minimumValue)
}

func continueTrackingWithTouch(touch: UITouch, event: UIEvent) -> Bool {
if !_minThumbOn && !_maxThumbOn {
return true
}

var touchPoint: CGPoint = touch.locationInView(self)


if (_minThumbOn != nil) {
_minThumb.center = CGPointMake(max(CGFloat(self.xForValue(minimumValue)),min(touchPoint.x - CGFloat(distanceFromCenter), CGFloat(self.xForValue(selectedMaximumValue - minimumRange)))),CGFloat(_minThumb.center.y))
selectedMinimumValue = self.valueForX(Float(_minThumb.center.x))
}

if (_maxThumbOn != nil) {
_maxThumb.center = CGPointMake(min(CGFloat(self.xForValue(maximumValue)), max(touchPoint.x - CGFloat(distanceFromCenter), CGFloat(self.xForValue(selectedMinimumValue + minimumRange)))), CGFloat(_maxThumb.center.y))
selectedMaximumValue = self.valueForX(Float(_maxThumb.center.x))
}

self.updateTrackHighlight()
self.setNeedsLayout()

self.sendActionsForControlEvents(UIControlEvents.ValueChanged)
return true
}

func beginTrackingWithTouch(touch: UITouch, event: UIEvent) -> Bool {
var touchPoint: CGPoint = touch.locationInView(self)

if(CGRectContainsPoint(_minThumb.frame, touchPoint)){
_minThumbOn = true;
distanceFromCenter = Float(touchPoint.x - _minThumb.center.x)
}
else if(CGRectContainsPoint(_maxThumb.frame, touchPoint)){
_maxThumbOn = true;
distanceFromCenter = Float(touchPoint.x - _maxThumb.center.x)

}
return true;


}

func endTrackingWithTouch(touch: UITouch, event: UIEvent) {
_minThumbOn = false;
_maxThumbOn = false;
}

func updateTrackHighlight() {
_track.frame = CGRectMake(
_minThumb.center.x,
_track.center.y - (_track.frame.size.height/2),
_maxThumb.center.x - _minThumb.center.x,
_track.frame.size.height
);
}


}

我的输出

enter image description here

应该是这样的

enter image description here

我该如何解决这个问题?

最佳答案

你的问题出在“让”上。

_minThumb = UIImageView(image: UIImage(named: "handle.png"), highlightedImage: UIImage(named: "handle-hover.png"))
_minThumb.frame = CGRectMake(0,0, self.frame.size.height,self.frame.size.height);
_minThumb.contentMode = UIViewContentMode.Center
self.addSubview(_minThumb)

_maxThumb = UIImageView(image: UIImage(named: "handle.png"), highlightedImage: UIImage(named: "handle-hover.png"))
_maxThumb.frame = CGRectMake(0,0, self.frame.size.height,self.frame.size.height)
_maxThumb.contentMode = UIViewContentMode.Center
self.addSubview(_maxThumb)

只需删除“let”,它就可以工作了,这是因为您正在全局使用 _minThumb 和 _maxThumb,但您将它们声明为常量,因此它们一旦初始化就不会响应任何更改。

关于ios - Swift - UISlider 的自定义控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30216374/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com