Custom UIImage components for circular covers, turning, and blurring backgrounds

Source: Internet
Author: User
Tags set background

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.