2015-11-10 41 views
1

我的代碼是用Swift 2.0編寫的,其部署目標是iOS 8.0或更高版本。我試圖擴展UIView,以便不使用UIView.animateWithDuration(),我可以使用我的自定義函數。我想採取一個圓形的UIView,並保持它的角部半徑仍然是其高度和寬度的一半,但是會填滿一個正方形。所以UIView應該擴展它的當前形狀,直到它達到矩形邊界。我想在UIView上使用擴展。如下。UIView的擴展

extension UIView { 
     func customAnimation() { 

     } 
} 

工作的代碼是。

let ViewFrame: CGRect = CGRect(x: 100, y: 100, width: 10, height: 10) 

let View: UIView = UIView(frame: ViewFrame) 

View.layer.cornerRadius = 5 
View.bounds = CGRect(x: 0, y: 0, width: 300, height: 300) 
View.layer.masksToBounds = true 
UIView.animateWithDuration(duration: 10.0, delay: 0.0, options: .CurveEaseInOut, animation: { 
    View.transform = CGAffineTransformMakeScale(100.0, 100.0) 
}, completion: nil) 

回答

0

你真的只需要動畫邏輯移入擴展功能這樣

extension UIView { 
    func customAnimtation() { 
     UIView.animateWithDuration(10.0, delay: 0.0, options: .CurveEaseInOut, animations: { 
      self.transform = CGAffineTransformMakeScale(100.0, 100.0) 
     }, completion: nil) 
    } 
} 


let ViewFrame: CGRect = CGRect(x: 100, y: 100, width: 10, height: 10) 
let View: UIView = UIView(frame: ViewFrame) 
View.layer.cornerRadius = 5 
View.bounds = CGRect(x: 0, y: 0, width: 300, height: 300) 
View.layer.masksToBounds = true 
View.customAnimtation() 
0
enum AIEdge:Int { 
case 
Top, 
Left, 
Bottom, 
Right, 
Top_Left, 
Top_Right, 
Bottom_Left, 
Bottom_Right, 
All, 
None 

}

extension UIView { 

//MARK:- HEIGHT/WIDTH 

var width:CGFloat { 
    return self.frame.size.width 
} 
var height:CGFloat { 
    return self.frame.size.height 
} 
var xPos:CGFloat { 
    return self.frame.origin.x 
} 
var yPos:CGFloat { 
    return self.frame.origin.y 
} 

//MARK:- DASHED BORDER 
func drawDashedBorderAroundView() { 
    let cornerRadius: CGFloat = self.frame.size.width/2 
    let borderWidth: CGFloat = 0.5 
    let dashPattern1: Int = 4 
    let dashPattern2: Int = 2 
    let lineColor = WHITE_COLOR 

    //drawing 
    let frame: CGRect = self.bounds 
    let shapeLayer = CAShapeLayer() 
    //creating a path 
    let path: CGMutablePath = CGMutablePath() 

    //drawing a border around a view 
    path.move(to: CGPoint(x: CGFloat(0), y: CGFloat(frame.size.height - cornerRadius)), transform: .identity) 
    path.addLine(to: CGPoint(x: CGFloat(0), y: CGFloat(cornerRadius)), transform: .identity) 
    path.addArc(center: CGPoint(x: CGFloat(cornerRadius), y: CGFloat(cornerRadius)), radius: CGFloat(cornerRadius), startAngle: CGFloat(M_PI), endAngle: CGFloat(-M_PI_2), clockwise: false, transform: .identity) 
    path.addLine(to: CGPoint(x: CGFloat(frame.size.width - cornerRadius), y: CGFloat(0)), transform: .identity) 
    path.addArc(center: CGPoint(x: CGFloat(frame.size.width - cornerRadius), y: CGFloat(cornerRadius)), radius: CGFloat(cornerRadius), startAngle: CGFloat(-M_PI_2), endAngle: CGFloat(0), clockwise: false, transform: .identity) 
    path.addLine(to: CGPoint(x: CGFloat(frame.size.width), y: CGFloat(frame.size.height - cornerRadius)), transform: .identity) 
    path.addArc(center: CGPoint(x: CGFloat(frame.size.width - cornerRadius), y: CGFloat(frame.size.height - cornerRadius)), radius: CGFloat(cornerRadius), startAngle: CGFloat(0), endAngle: CGFloat(M_PI_2), clockwise: false, transform: .identity) 
    path.addLine(to: CGPoint(x: CGFloat(cornerRadius), y: CGFloat(frame.size.height)), transform: .identity) 
    path.addArc(center: CGPoint(x: CGFloat(cornerRadius), y: CGFloat(frame.size.height - cornerRadius)), radius: CGFloat(cornerRadius), startAngle: CGFloat(M_PI_2), endAngle: CGFloat(M_PI), clockwise: false, transform: .identity) 

    //path is set as the _shapeLayer object's path 

    shapeLayer.path = path 
    shapeLayer.backgroundColor = UIColor.clear.cgColor 
    shapeLayer.frame = frame 
    shapeLayer.masksToBounds = false 
    shapeLayer.setValue(NSNumber(value: false), forKey: "isCircle") 
    shapeLayer.fillColor = UIColor.clear.cgColor 
    shapeLayer.strokeColor = lineColor.cgColor 
    shapeLayer.lineWidth = borderWidth 
    shapeLayer.lineDashPattern = [NSNumber(integerLiteral: dashPattern1),NSNumber(integerLiteral: dashPattern2)] 
    shapeLayer.lineCap = kCALineCapRound 

    self.layer.addSublayer(shapeLayer) 
    //self.layer.cornerRadius = cornerRadius 
} 


//MARK:- ROTATE 
func rotate(angle: CGFloat) { 
    let radians = angle/180.0 * CGFloat(M_PI) 
    self.transform = self.transform.rotated(by: radians); 
} 




//MARK:- BORDER 
func applyBorderDefault() { 
    self.applyBorder(color: UIColor.red, width: 1.0) 
} 
func applyBorderDefault1() { 
    self.applyBorder(color: UIColor.green, width: 1.0) 
} 
func applyBorderDefault2() { 
    self.applyBorder(color: UIColor.blue, width: 1.0) 
} 
func applyBorder(color:UIColor, width:CGFloat) { 
    self.layer.borderColor = color.cgColor 
    self.layer.borderWidth = width 
} 


//MARK:- CIRCLE 
func applyCircle() { 
    self.layer.cornerRadius = min(self.frame.size.height, self.frame.size.width) * 0.5 
    self.layer.masksToBounds = true 
    self.clipsToBounds = true 
} 
func applyCircleWithRadius(radius:CGFloat) { 
    self.layer.cornerRadius = radius 
    self.layer.masksToBounds = true 
} 

//MARK:- CORNER RADIUS 
func applyCornerRadius(radius:CGFloat) { 
    self.layer.cornerRadius = radius 
    self.layer.masksToBounds = true 
} 

func applyCornerRadiusDefault() { 
    self.applyCornerRadius(radius: 5.0) 
} 


//MARK:- SHADOW 
func applyShadowDefault() { 
    self.applyShadowWithColor(color: UIColor.black, opacity: 0.5, radius: 1) 
} 

func applyShadowWithColor(color:UIColor) { 
    self.applyShadowWithColor(color: color, opacity: 0.5, radius: 1) 
} 

func applyShadowWithColor(color:UIColor, opacity:Float, radius: CGFloat) { 
    self.layer.shadowColor = color.cgColor 
    self.layer.shadowOpacity = opacity 
    self.layer.shadowOffset = CGSize.zero 
    self.layer.shadowRadius = radius 
    self.clipsToBounds = false 
} 



func applyShadowWithColor(color:UIColor, opacity:Float, radius: CGFloat, edge:AIEdge, shadowSpace:CGFloat) { 

    var sizeOffset:CGSize = CGSize.zero 
    switch edge { 
    case .Top: 
     sizeOffset = CGSize(width: 0, height: -shadowSpace) //CGSizeMake(0, -shadowSpace) 
    case .Left: 
     sizeOffset = CGSize(width: -shadowSpace, height: 0) //CGSizeMake(-shadowSpace, 0) 
    case .Bottom: 
     sizeOffset = CGSize(width: 0, height: shadowSpace) //CGSizeMake(0, shadowSpace) 
    case .Right: 
     sizeOffset = CGSize(width: shadowSpace, height: 0) //CGSizeMake(shadowSpace, 0) 


    case .Top_Left: 
     sizeOffset = CGSize(width: -shadowSpace, height: -shadowSpace) //CGSizeMake(-shadowSpace, -shadowSpace) 
    case .Top_Right: 
     sizeOffset = CGSize(width: shadowSpace, height: -shadowSpace) //CGSizeMake(shadowSpace, -shadowSpace) 
    case .Bottom_Left: 
     sizeOffset = CGSize(width: -shadowSpace, height: shadowSpace) //CGSizeMake(-shadowSpace, shadowSpace) 
    case .Bottom_Right: 
     sizeOffset = CGSize(width: shadowSpace, height: shadowSpace) //CGSizeMake(shadowSpace, shadowSpace) 


    case .All: 
     sizeOffset = CGSize(width: 0, height: 0) //CGSizeMake(0, 0) 
    case .None: 
     sizeOffset = CGSize.zero 
    } 

    self.layer.shadowColor = color.cgColor 
    self.layer.shadowOpacity = opacity 
    self.layer.shadowOffset = sizeOffset 
    self.layer.shadowRadius = radius 
    self.clipsToBounds = false 
} 


func addBorderWithColor(color:UIColor, edge:AIEdge, thicknessOfBorder:CGFloat) { 

    // dispatch_async(dispatch_get_main_queue()) { 

    DispatchQueue.main.async { 

     var rect:CGRect = CGRect.zero 

     switch edge { 
     case .Top: 
      rect = CGRect(x: 0, y: 0, width: self.width, height: thicknessOfBorder) //CGRectMake(0, 0, self.width, thicknessOfBorder); 
     case .Left: 
      rect = CGRect(x: 0, y: 0, width: thicknessOfBorder, height:self.width) //CGRectMake(0, 0, thicknessOfBorder, self.height); 
     case .Bottom: 
      rect = CGRect(x: 0, y: self.height - thicknessOfBorder, width: self.width, height: thicknessOfBorder) //CGRectMake(0, self.height - thicknessOfBorder, self.width, thicknessOfBorder); 
     case .Right: 
      rect = CGRect(x: self.width-thicknessOfBorder, y: 0, width: thicknessOfBorder, height: self.height) //CGRectMake(self.width-thicknessOfBorder, 0,thicknessOfBorder, self.height); 
     default: 
      break 
     } 

     let layerBorder = CALayer() 
     layerBorder.frame = rect 
     layerBorder.backgroundColor = color.cgColor 
     self.layer.addSublayer(layerBorder) 
    } 
} 


func animateVibrate() { 

    let duration = 0.05 

    UIView.animate(withDuration: duration , 
           animations: { 
           self.transform = self.transform.translatedBy(x: 5, y: 0) 
    }, 
           completion: { finish in 

           UIView.animate(withDuration: duration , 
                  animations: { 
                  self.transform = self.transform.translatedBy(x: -10, y: 0) 
           }, 
                  completion: { finish in 


                  UIView.animate(withDuration: duration , 
                         animations: { 
                         self.transform = self.transform.translatedBy(x: 10, y: 0) 
                  }, 
                         completion: { finish in 


                         UIView.animate(withDuration: duration , 
                                animations: { 
                                self.transform = self.transform.translatedBy(x: -10, y: 0) 
                         }, 
                                completion: { finish in 

                                UIView.animate(withDuration: duration){ 
                                 self.transform = CGAffineTransform.identity 
                                } 
                         }) 
                  }) 
           }) 
    }) 
} 

}