更新日期:2017年03月12日
對(duì)于UIView的一些 extension 方法(動(dòng)畫等...)
1.平移動(dòng)畫
/// 視圖位移 x
///
/// - Parameters:
/// - x: 坐標(biāo)點(diǎn)x
/// - duration: 動(dòng)畫時(shí)間
func moveViewWithAnimateXPoint(XPoint x:CGFloat , duration:CGFloat) ->Void {
UIView.animate(withDuration: TimeInterval(duration), delay: 0, options: .curveEaseInOut, animations: {
// 動(dòng)畫操作
self.frame.origin.x = x
}, completion: nil)
}
/// 視圖位移 y
///
/// - Parameters:
/// - y: 坐標(biāo)點(diǎn)y
/// - duration: 動(dòng)畫時(shí)間
func moveViewWithAnimateYPoint(YPoint y:CGFloat , duration:CGFloat) -> Void {
UIView.animate(withDuration: TimeInterval(duration), delay: 0, options: .curveEaseInOut, animations: {
// 動(dòng)畫操作
self.frame.origin.y = y
}, completion: nil)
}
/// 根據(jù)坐標(biāo)點(diǎn)進(jìn)行位移動(dòng)畫
///
/// - Parameters:
/// - point: CGPoint
/// - duration: 動(dòng)畫時(shí)長(zhǎng)
func moveViewWithAnimateToPoint(Point point:CGPoint ,duration:CGFloat ) -> Void {
self.moveViewWithAnimateXPoint(XPoint: point.x, duration: duration)
self.moveViewWithAnimateYPoint(YPoint: point.y, duration: duration)
}
/// 根據(jù)視圖中心點(diǎn)進(jìn)行位移
///
/// - Parameters:
/// - point: center
/// - duration: 動(dòng)畫時(shí)長(zhǎng)
func moveViewWithAnimateToCenterPoint(CenterPoint point:CGPoint , duration:CGFloat ) -> Void {
UIView.animate(withDuration: TimeInterval(duration), delay: 0, options: .curveEaseInOut, animations: {
// 動(dòng)畫操作
self.center = point
}, completion: nil)
}
對(duì)于UIImage的一些擴(kuò)展
1.圖片壓縮
/// 圖片的寬
var width:CGFloat { return self.size.width }
/// 圖片的高
var height:CGFloat { return self.size.height }
/// 壓縮圖片
///
/// - Parameter costomWidth: 指定寬度
/// - Returns: 壓縮過后的圖片
func imageReduceSize(byWidth costomWidth:CGFloat) -> UIImage {
let costomHeight = (costomWidth / width) * height
UIGraphicsBeginImageContext(CGSize(width: costomWidth, height: costomHeight))
self.draw(in: CGRect(x: 0, y: 0, width: costomWidth, height: costomHeight))
let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return newImage
}