Sometimes we need to do some extensions to iOS components, such as adding a property or a method to achieve our ulterior purpose, and then we need to write a subclass that inherits the iOS component and then expands on the subclass. Here's an example of how to use the UIImage subclass to crop, frame, and rotate a circular picture.
Below is a music player's music playback interface, we want to cut the cover into a circle and realize the function of rotating when playing music:
Create a new class that uses the Swift language:
The code is as follows, and the comments are detailed:
Import UIKit
Class Ekoimage:uiimageview {
Write first construct method
Required Init (coder Adecoder:nscoder) {
Calling the parent class's constructor method
Super.init (Coder:adecoder)
Set rounded corners
Self.clipstobounds = True
Self.layer.cornerRadius = SELF.FRAME.SIZE.WIDTH/2
Set border
Self.layer.borderWidth = 4
Self.layer.borderColor = Uicolor (red:1.0, green:1.0, blue:1.0, alpha:0.7). Cgcolor//Considering that you want to set the transparency, use this method
}
Implementing the Rotation method
Func onrotation () {
Animation instance Keywords
var animation = cabasicanimation (keypath: "Transform.rotation")
Initial value
Animation.fromvalue = 0.0
End Value
Animation.tovalue = 2*m_pi
Animation Execution Time
Animation.duration = 20
Animation execution times
Animation.repeatcount = 10000
Assigning an animated instance to a layer
Self.layer.addAnimation (animation, Forkey:nil)
}
}
Link the album cover and the time background to the newly written class:
Run it and cut it to a circle!
Why can't you spin it? Also call the rotation method when the view loads:
Run again, rotate successfully! ~
Slightly small skill, but also can add background blur effect:
The code is as follows:
Set background blur
Let Blureffect = Uiblureffect (style:UIBlurEffectStyle.Light)
Let Blurview = Uivisualeffectview (effect:blureffect)
BlurView.frame.size = Cgsize (Width:view.frame.width, Height:view.frame.height)
Bg.addsubview (Blurview)
Custom UIImage components implement round cover, rotate, and blur background