swift 自定义弹框

时间:2023-03-10 06:50:08
swift 自定义弹框

//

//  ViewController.swift

//  animationAlert

//

//  Created by su on 15/12/9.

//  Copyright © 2015年 tian. All rights reserved.

//

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {

super.viewDidLoad()

//灰色的遮挡板

let overLayView = UIView(frame: self.view.bounds)

overLayView.backgroundColor = UIColor.blackColor()

overLayView.alpha = 0

//加入场景

self.view.addSubview(overLayView)

//警告框相关的代码

//警告框的宽度

let alerDimension:CGFloat = 250

let alertViewFrame = CGRect(x: self.view.bounds.size.width / 2 - alerDimension / 2, y: self.view!.bounds.size.height / 2 - alerDimension / 2, width: alerDimension, height: alerDimension)

let alertView  = UIView(frame: alertViewFrame)

alertView.backgroundColor = UIColor(patternImage: UIImage(named: "alert_box")!)

alertView.alpha = 1

//警告框初始尺寸为1.2 倍

alertView.transform = CGAffineTransformMakeScale(1.2, 1.2)

//设置圆角半径

alertView.layer.cornerRadius = 10

//设置阴影

//颜色

alertView.layer.shadowColor = UIColor.blackColor().CGColor

//阴影偏移

alertView.layer.shadowOffset = CGSizeMake(0, 5)

//阴影透明度

alertView.layer.shadowOpacity = 0

//阴影的半径

alertView.layer.shadowRadius = 10

self.view.addSubview(alertView)

//延时设置

var minseconds = 1 * Double(NSEC_PER_MSEC)

var dtime = dispatch_time(DISPATCH_TIME_NOW, Int64(minseconds))

dispatch_after(dtime, dispatch_get_main_queue()) { () -> Void in

//自定义警告框动画的上半部分

UIView.animateWithDuration(0.3, delay: 0, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in

overLayView.alpha = 0.3

alertView.alpha = 1

}, completion: nil)

let scale = JNWSpringAnimation(keyPath: "transform.scale")

scale.damping = 14

scale.stiffness = 14

scale.mass = 1

scale.fromValue = 1.2

scale.toValue = 1

alertView.layer.addAnimation(scale, forKey: scale.keyPath)

alertView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1, 1)

}

minseconds = 3 * Double(NSEC_PER_MSEC)

dtime = dispatch_time(DISPATCH_TIME_NOW, Int64(minseconds))

dispatch_after(dtime, dispatch_get_main_queue()) { () -> Void in

//下半部分消失

UIView.animateWithDuration(1, delay: 0, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in

overLayView.alpha = 0

alertView.alpha = 0

}, completion: nil)

let scaleOut = JNWSpringAnimation(keyPath: "tranform.scale")

scaleOut.damping = 14

scaleOut.stiffness = 14

scaleOut.mass = 1

scaleOut.fromValue = 1

scaleOut.toValue = 0.7

alertView.layer.addAnimation(scaleOut, forKey: scaleOut.keyPath)

alertView.transform = CGAffineTransformMakeScale(0.7, 0.7)

}

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}

}