Swift extension example

后端 未结 8 2111
既然无缘
既然无缘 2020-11-28 01:33

I was originally wanting to know how to make something like this

UIColor.myCustomGreen

so that I could define my own colors and use them th

相关标签:
8条回答
  • 2020-11-28 02:25

    UIColor+util.swift

    import UIKit
    
    
    extension UIColor{
    
    
        class func getCustomBlueColor() -> UIColor
        {
            return UIColor(red:0.043, green:0.576 ,blue:0.588 , alpha:1.00)
        }
    
        func getNameofColour() ->String
        {
            return "myOrange"
        }
    
    }
    

    Usage :

    NSLog("\(UIColor.getCustomBlueColor())")
    let color=UIColor(red:0.043, green:0.576 ,blue:0.588 , alpha:1.00);
    NSLog(color.getNameofColour())
    

    I hope you see that what is difference . One of Function starting with class func another one starting only func . you can use which you like.

    0 讨论(0)
  • 2020-11-28 02:25

    Here is an extension example of an eye catching animation effect that works with cells from UITableView. Each cell grows from a point source to normal size as you scroll a UITableView. Adjust the animation timing as desired.

    Since each cell shows up with a little time stagger while scrolling, the effect ripples nicely! See this 15 second clip that showcases the effect : https://www.youtube.com/watch?v=BVeQpno56wU&feature=youtu.be

    
    extension UITableViewCell {
    
        func growCellDuringPresentation(thisCell : UITableViewCell) {
    
            thisCell.transform = CGAffineTransform(scaleX: 0.01, y: 0.01)
    
            UIView.animate(withDuration: TimeInterval(0.35), delay: 0.0, options: UIView.AnimationOptions.allowUserInteraction,   animations: {
    
                thisCell.transform = CGAffineTransform(scaleX: 1, y: 1)
    
            }, completion: nil)
    
        }
    }
    
    

    To use the extension you make a call to it just before the cell is returned in cellForRowAt, like shown below :

    
                cell.growCellDuringPresentation(thisCell: cell)
                return cell
    

    Note this same method works when returning cells for a collection view.

    Here is an extension that works exactly the same, except that it rotates the cells during presentation :

    
    extension UITableViewCell {
    
        func rotateCellDuringPresentation(thisCell : UITableViewCell) {
    
            thisCell.transform = CGAffineTransform(rotationAngle: .pi)
    
            UIView.animate(withDuration: TimeInterval(0.35), delay: 0.0, options: UIView.AnimationOptions.allowUserInteraction,   animations: {
    
                thisCell.transform = CGAffineTransform(rotationAngle: 0)
    
            }, completion: nil)
    
        }
    }
    
    

    It's called similarly :

    
                cell.rotateCellDuringPresentation(thisCell: cell)
                return cell
    

    Here is an extension along the same lines that translates the cells in the X direction

    
    extension UITableViewCell {
    
        func translateCellDuringPresentation(thisCell : UITableViewCell) {
    
            thisCell.layer.transform = CATransform3DMakeTranslation(-300, 0, 0)
    
            UIView.animate(withDuration: TimeInterval(0.5), delay: 0.0, options: UIView.AnimationOptions.allowUserInteraction,   animations: {
    
                thisCell.layer.transform = CATransform3DMakeTranslation(0, 0, 0)
    
            }, completion: nil)
    
        }
    }
    

    It's called similarly :

    
                cell.translateCellDuringPresentation(thisCell: cell)
                return cell
    
    0 讨论(0)
提交回复
热议问题