Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 103 additions & 7 deletions SweetAlert/SweetAlert.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import UIKit
import QuartzCore

public enum AlertStyle {
case success,error,warning,none
case customImage(imageFile:String)
case success,error,warning,none,loading
case customImag(imageFile:String)
}

open class SweetAlert: UIViewController {
Expand Down Expand Up @@ -106,7 +106,7 @@ open class SweetAlert: UIViewController {
}

// Subtitle
if self.subTitleTextView.text.isEmpty == false {
if !self.subTitleTextView.text.isEmpty {
let subtitleString = subTitleTextView.text! as NSString
let rect = subtitleString.boundingRect(with: CGSize(width: width, height: 0.0), options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: [NSFontAttributeName:subTitleTextView.font!], context: nil)
textViewHeight = ceil(rect.size.height) + 10.0
Expand Down Expand Up @@ -178,7 +178,7 @@ open class SweetAlert: UIViewController {

func closeAlert(_ buttonIndex:Int){
if userAction != nil {
let isOtherButton = buttonIndex == 0 ? true: false
let isOtherButton = buttonIndex == 0
SweetAlertContext.shouldNotAnimate = true
userAction!(isOtherButton)
SweetAlertContext.shouldNotAnimate = false
Expand Down Expand Up @@ -255,6 +255,9 @@ open class SweetAlert: UIViewController {
case .error:
self.animatedView = CancelAnimatedView()

case .loading:
self.animatedView = LoadingAnimatedView()

case .warning:
self.animatedView = InfoAnimatedView()

Expand All @@ -271,7 +274,7 @@ open class SweetAlert: UIViewController {
self.subTitleTextView.text = subTitle
}
buttons = []
if buttonTitle.isEmpty == false {
if !buttonTitle.isEmpty {
let button: UIButton = UIButton(type: UIButtonType.custom)
button.setTitle(buttonTitle, for: UIControlState())
button.backgroundColor = buttonColor
Expand All @@ -280,7 +283,7 @@ open class SweetAlert: UIViewController {
buttons.append(button)
}

if otherButtonTitle != nil && otherButtonTitle!.isEmpty == false {
if otherButtonTitle != nil && !otherButtonTitle!.isEmpty {
let button: UIButton = UIButton(type: UIButtonType.custom)
button.setTitle(otherButtonTitle, for: UIControlState())
button.backgroundColor = otherButtonColor
Expand All @@ -290,7 +293,7 @@ open class SweetAlert: UIViewController {
}

resizeAndRelayout()
if SweetAlertContext.shouldNotAnimate == true {
if SweetAlertContext.shouldNotAnimate {
//Do not animate Alert
if self.animatedView != nil {
self.animatedView!.animate()
Expand Down Expand Up @@ -345,6 +348,99 @@ class AnimatableView: UIView {
}
}

class LoadingAnimatedView: AnimatableView{
var circleLayer: CAShapeLayer = CAShapeLayer()
private var circlePath: CGPath {
let path = UIBezierPath()
let startAngle: CGFloat = 0
let endAngle: CGFloat = 2*M_PI
path.addArcWithCenter(CGPointMake(self.frame.size.width/2.0, self.frame.size.width/2.0), radius: self.frame.size.width/2.0, startAngle: startAngle, endAngle: endAngle, clockwise: true)
return path.CGPath
}
override required init(frame: CGRect) {
super.init(frame: frame)
setupLayers()
}
override func layoutSubviews() {
setupLayers()
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func setupLayers(){
circleAnimation.path = circlePath
circleAnimation.fillColor=UIColor.clearColor().CGColor
circleAnimation.strokeColor=UIColor.colorFromRGB(0x2960FF).CGColor
circleAnimation.lineCap=kCALineCapRound
circleAnimation.lineWidth=4
circleAnimation.frame=CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height)
circleAnimation.position=CGPoint(x: self.frame.size.width/2.0, y: self.frame.size.height/2.0)
layer.addSublayer(circleAnimation)
CATransaction.begin()
circleAnimation.strokeStart=0.25
CATransaction.commit()
}
override func animate(){
let animation=CABasicAnimation(keyPath: "transform.rotation")
animation.duration=2.0
animation.byValue=NSNumber(double: 2*M_PI )
animation.repeatCount = Float.infinity

let shrinkAnimation = CABasicAnimation(keyPath: "strokeStart")
shrinkAnimation.duration=0.75
shrinkAnimation.removedOnCompletion=false
shrinkAnimation.fromValue=0.25
shrinkAnimation.toValue=0.9999
shrinkAnimation.fillMode=kCAFillModeForwards
shrinkAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)

let moveToStart = CABasicAnimation(keyPath: "strokeStart")
moveToStart.duration=0.001
moveToStart.removedOnCompletion=false
moveToStart.fromValue=0
moveToStart.toValue=0
moveToStart.beginTime=0.75
moveToStart.fillMode=kCAFillModeForwards
moveToStart.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)

let moveEndToStart = CABasicAnimation(keyPath: "stokeEnd")
moveEndToStart.duration=0.001
moveEndToStart.removedOnCompletion=false
moveEndToStart.fromValue=0
moveEndToStart.toValue=0.001
moveEndToStart.beginTime=0.75
moveEndToStart.fillMode=kCAFillModeForwards
moveEndToStart.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)

let moveStartAnimation = CABasicAnimation(keyPath: "strokeStart")
moveStartAnimation.duration=0.75
moveStartAnimation.removedOnCompletion=false
moveStartAnimation.fromValue=0
moveStartAnimation.toValue=0.25
moveStartAnimation.beginTime=0.7501
moveStartAnimation.fillMode=kCAFillModeForwards
moveStartAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)

let growAnimation = CABasicAnimation(keyPath: "strokeEnd")
growAnimation.duration=0.75
growAnimation.removedOnCompletion=false
growAnimation.fromValue=0.001
growAnimation.toValue=1
growAnimation.beginTime=0.7501
growAnimation.fillMode=kCAFillModeForwards
growAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)

let groupAnimations = CAAnimationGroup()
groupAnimations.animations = [shrinkAnimation,moveEndToStart,moveToStart,growAnimation,moveStartAnimation]
groupAnimations.repeatCount=Float.infinity
groupAnimations.duration=1.5001
groupAnimations.fillMode=kCAFillModeForwards

circleAnimation.add(groupAnimations, forKey: "drawAnimation")
circleAnimation.add(animation, forKey: "rotateAnimation")
}
}

class CancelAnimatedView: AnimatableView {

var circleLayer = CAShapeLayer()
Expand Down