swift3.0学习与累积

2017-03-12  本文已影响0人  please_smile

更新日期:2017年03月12日

对于UIView的一些 extension 方法(动画等...)

1.平移动画

 /// 视图位移 x
    ///
    /// - Parameters:
    ///   - x: 坐标点x
    ///   - duration: 动画时间
    func moveViewWithAnimateXPoint(XPoint x:CGFloat , duration:CGFloat) ->Void {
        
        UIView.animate(withDuration: TimeInterval(duration), delay: 0, options: .curveEaseInOut, animations: {
            // 动画操作
            self.frame.origin.x = x
            
        }, completion: nil)
        
    }
    /// 视图位移 y
    ///
    /// - Parameters:
    ///   - y: 坐标点y
    ///   - duration: 动画时间
    func moveViewWithAnimateYPoint(YPoint y:CGFloat , duration:CGFloat) -> Void {
        UIView.animate(withDuration: TimeInterval(duration), delay: 0, options: .curveEaseInOut, animations: {
            // 动画操作
            self.frame.origin.y = y
            
        }, completion: nil)

    }
    /// 根据坐标点进行位移动画
    ///
    /// - Parameters:
    ///   - point: CGPoint
    ///   - duration: 动画时长
    func moveViewWithAnimateToPoint(Point point:CGPoint ,duration:CGFloat ) -> Void {
        self.moveViewWithAnimateXPoint(XPoint: point.x, duration: duration)
        self.moveViewWithAnimateYPoint(YPoint: point.y, duration: duration)
    }

    /// 根据视图中心点进行位移
    ///
    /// - Parameters:
    ///   - point: center
    ///   - duration: 动画时长
    func moveViewWithAnimateToCenterPoint(CenterPoint point:CGPoint , duration:CGFloat ) -> Void {
        UIView.animate(withDuration: TimeInterval(duration), delay: 0, options: .curveEaseInOut, animations: {
            // 动画操作
            self.center = point
        }, completion: nil)
    }

对于UIImage的一些扩展

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
    }

上一篇 下一篇

猜你喜欢

热点阅读