2017-10-09 118 views
-1

我需要一个带有进度条的uibutton,当它完成时,我需要类似完成处理程序在完成bar之后执行动作。我搜索了很多,我刚刚找到一个没有完成处理程序的吊舱。有人会建议一个合适的库吗?Swift - 进度条按钮

这是我试过,但它有一些缺陷:

public class ProgressingBarButton: UIButton { 

//vars ***** 
private var buttonWidth: CGFloat { 
    return self.bounds.width 
} 

private var buttonHeight: CGFloat { 
    return self.bounds.height 
} 

private var buttonX: CGFloat { 
    return self.frame.origin.x 
} 

private var buttonY: CGFloat { 
    return self.frame.origin.y 
} 

private var buttonRect: CGRect { 
    return CGRect(x: 0, y: 0, width: buttonWidth, height: buttonHeight) 
} 

private var progressingBar = UIView() 

@IBInspectable var progressingBarColor: UIColor { 
    didSet { 
     progressingBar.backgroundColor = progressingBarColor 
    } 
} 




var titleFontName = "Helvetica" 

@IBInspectable var FontName: String { 
    didSet { 
     titleFontName = FontName 
    } 
} 


@IBInspectable var radius: CGFloat = 0.0 { 
    didSet { 
     self.layer.cornerRadius = radius 
     self.clipsToBounds = true 
    } 
} 

var button = UIButton() 
var timer = Timer() 


//timer Done caller 
//public var didTimePeriodElapseBlock : (() -> Void) = {() -> Void in } 

//init ***** 
required public init?(coder aDecoder: NSCoder) { 
    self.progressingBarColor = UIColor.white 

    FontName = "Helvetica" 
    super.init(coder: aDecoder) 
    initialPrgressingBar() 
    //fatalError("init(coder:) has not been implemented") 
} 

override public init(frame: CGRect) { 

    self.progressingBarColor = UIColor.white 
    FontName = "Helvetica" 

    super.init(frame: frame) 
    initialPrgressingBar() 
} 


override public func awakeFromNib() { 
    button.addTarget(self, action: #selector(self.start), for: .touchDown) 
    button.addTarget(self, action: #selector(self.cancel), for: .touchUpInside) 
    button.addTarget(self, action: #selector(self.cancel), for: .touchCancel) 
    button.addTarget(self, action: #selector(self.cancel), for: .touchDragExit) 
    button.addTarget(self, action: #selector(self.cancel), for: .touchDragOutside) 


    self.addSubview(progressingBar) 
    self.addSubview(button) 
    initialPrgressingBar() 

} 



public override func draw(_ rect: CGRect) { 
    initialPrgressingBar() 
} 


//func ******** 
func initialPrgressingBar() { 

    UIView.animate(withDuration: 0.3) { 
     self.progressingBar.frame = CGRect(x: 0, y: 0, width: 0, height: self.buttonHeight) 
    } 

    button.frame = buttonRect 
} 


//****** 
@objc func start() { 

    timer.invalidate() 
    self.timer = Timer(timeInterval: 0.9, target: self, selector: #selector(void), userInfo: nil, repeats: false) 
    progressingBar.frame.size.width = 0 

    UIView.animate(withDuration: 0.9, animations: { 

     self.progressingBar.frame.size.width = self.frame.width 
    }) { (_) in 

     if self.timer.isValid { 
      print("complited") 
      NotificationCenter.default.post(name: .readyBtnCompleted, object: nil) 
      //self.didTimePeriodElapseBlock() 

     } 


    } 

} 

//**** 
@objc private func cancel() { 
    reset() 
    delay() 
} 

//***** 
@objc private func reset() { 

    if timer.isValid { 
     timer.invalidate() 
    } 
    initialPrgressingBar() 

} 

//*** 
private func delay() { 

    self.isUserInteractionEnabled = false 
    DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) { 
     self.isUserInteractionEnabled = true 
    } 

} 

//**** 
@objc private func void() {} 

}

没有任何第三方libarary为?

+0

显示你已经尝试过,并明确说明您的具体要求。 – PGDev

+0

@PGDev https://github.com/emadhegab/MHProgressButton ....需要知道当栏完成后触发某些操作。 – Mehrdad

+0

我认为你添加的链接是第三方库。添加迄今为止完成的代码。 – PGDev

回答

0

您需要修改库方法:

func update() { 

     self.progressButton.linearLoadingWith(progress: CGFloat(i)) 
     i += 1 
     if i > 100 { 
      timer?.invalidate() 
      //Add your code here 
     } 
    } 
+0

对不起,从库我的意思是第三方库:D – Mehrdad

+0

你可以简单地使用苹果的UIProgressView这种功能。 – PGDev

+0

它有完成处理程序? @PGDev – Mehrdad