高性能和轻量级的UIView,UIImage,UIImageView,UIlabel,UIButton,Promise等

斯威夫特用户界面棒

构建状态 兼容可可豆荚 平台 斯威夫特 4.0+

高性能和轻量级的UIView,UIImage,UIImageView,UIlabel,UIButton等。

特征

  • [x] SwiftyView GPU 渲染图像和颜色
  • [x] SwiftyColor — 颜色来自十六进制,颜色RGBA值来自UIColor,颜色来自图像
  • [x] 用于膨胀/缩放/舍入的 UIImage 扩展
  • [x] 自动清除内存中映像缓存
  • [x] SwiftyImageView 扩展 10+ 动画
  • [x] SwiftyImageView 150% 比 UIImageView 更高的高性能,具体取决于 UIView-package、Image-GPU 和 Image-Cache
  • [x] SwiftyLabel 300% 比 UIlabel 更高的高性能,取决于 UIView-package 和 TextKit
  • [x] SwiftyButton 300% 比 UIButton 更高的高性能,具体取决于 UIControl-package、TextKit 和 BackgroundImage-Advanced
  • [x] SwiftyToast依赖于globalCenter,并显示没有重叠的Toast。
  • [x] SwiftyAlert 包含 SuccessAlert、ErrorAlert、WarningAlert、InfoAlert、EditAlert 及其特殊样式。
  • [x] 轻量级,几乎每个 UI 对应一个类
  • [x] UI 加载线程安全
  • [x] 块包更易于使用
  • [x] 简单易用,所有 API 都与系统 API 相同
  • [x] SwiftyThreadPool 自动管理线程依赖于活动的 CPU,并在内部自动释放 Runloop
  • [x] SwiftyPromise是PromiseKit的轻量级版本,部分基于Javascript的A+规范,依赖于ThreadPool,一个有趣的功能是它可以在一个Promise中同时在主线程和后台。then

要求

  • iOS 8.0+
  • Xcode 9.0+
  • 斯威夫特 5.0+

通信

  • 如果发现错误,请打开问题。
  • If you have a feature request, open an issue.
  • If you want to contribute, submit a pull request.

Installation

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

$ gem install cocoapods
Bash

CocoaPods 1.1+ is required.

To integrate SwiftyUI into your Xcode project using CocoaPods, specify it in your :Podfile

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!

target '<Your Target Name>' do
    pod 'SwiftyUI'
end
Ruby

Then, run the following command:

$ pod install
Bash

Usage

SwiftyView

SwiftyView have a auto GPU rendering on color and Image showing.

import SwiftyUI

let myView : SwiftyView = SwiftyView().addTo(view)
myView.frame = CGRect.init(x: 50, y: 50, width: 100, height: 100)
Swift

You can also invoke UIView function directly, it also have most of the SwiftyView performance feature.

But if you want to have complete benefits, I suggest you to use SwiftyView, and it actually inherits from UIView.

let myView : UIView = UIView()
view.addSubview(myView)
myView.frame = CGRect.init(x: 50, y: 50, width: 100, height: 100)
Swift

SwiftyColor

color from Hex

colorRGBA value from UIColor

import SwiftyUI

let myColor: UIColor = .hex(0xccff00) // .hex("333399")
let redFloat: CGFloat = myColor.redValue //greenValue, blueValue, alphaValue
Swift

colors from Image, also return block is on main thread:

import SwiftyUI

myImage?.colors({ (background, primary, secondary, detail) in
    print("background color: \(background)")
    print("primary color: \(primary)")
    print("secondary color: \(secondary)")
    print("detail color: \(detail)")
})
Swift

UIImage Extensions

There are several extensions designed to make the common image manipulation operations as simple as possible.UIImage

Inflation

let myImage : UIImage? = UIImage.load("aImage")
myImage.inflate()
Swift

Inflating compressed image formats (such as PNG or JPEG) in a background queue can significantly improve drawing performance on the main thread.

Scaling

let myImage : UIImage? = UIImage.load("aImage")
let size = CGSize(width: 100.0, height: 100.0)

let scaledImage = myImage.reSize(to: size)

let scaledToFitImage = myImage.reSize(toFit: size)

let scaledToFillImage = myImage.reSize(toFill: size)
Swift

Rounded Corners

let myImage : UIImage? = UIImage.load("aImage")
let radius: CGFloat = 10.0

let roundedImage = myImage.rounded(withCornerRadius: radius)
let circularImage = myImage.roundedIntoCircle()
Swift

Image Cache

The in SwiftyUI fills the role of that additional caching layer. It is an in-memory image cache used to store images up to a given memory capacity. When the memory capacity is reached, the image cache is sorted by last access date, then the oldest image is continuously purged until the preferred memory usage after purge is met. Each time an image is accessed through the cache, the internal access date of the image is updated.ImageCachePool

let imageCachePool : ImageCachePool = .defalut
Swift

Add / Remove / Fetch Images

Interacting with the protocol APIs is very straightforward.ImageCache

let imageCachePool : ImageCachePool = .defalut
let myImage : UIImage? = UIImage.load("aImage")

imageCachePool.add(myImage, withIdentifier: "myImage")

let cachedMyImage = imageCachePool.image(withIdentifier: "myImage")

imageCachePool.removeImage(withIdentifier: "myImage")
Swift

SwiftyImageView

SwiftyImagView inherits from UIView and ImageSettable Protocol and its extension. Also has a better performance.Yet to provide the foundation of the extension. Due to the powerful support of these classes, protocols and extensions, the APIs are concise, easy to use and contain a large amount of functionality.SwiftyImagViewSwiftyImagView

let myImage : UIImage? = UIImage.load("btnBG")
let myImageView : SwiftyImageView = SwiftyImageView(myImage).addTo(view)
myImageView.frame = CGRect.init(x: 50, y: 150 + 20, width: 100, height: 100)
Swift

SwiftyImageView Image Transitions

By default, there is no image transition animation when setting the image on the image view. If you wish to add a cross dissolve or flip-from-bottom animation, then specify an with the preferred duration.ImageTransition

let myImage : UIImage? = UIImage.load("btnBG")
let myImageView : SwiftyImageView = SwiftyImageView(myImage).addTo(view)
myImageView.frame = CGRect.init(x: 50, y: 150 + 20, width: 100, height: 100)

let myTransition : SwiftyImageView.ImageTransition = .flipFromBottom(0.2)
        
myImageView.transition(myTransition, with: UIImage.load("aImage")!)
Swift

SwiftyLabel

SwiftyLabel is a better performance than UILabel and can be used like a standard UI component. Also, Easier to use than UILabel. Since UIView is inherited instead of UILabel, there is little wasteful processing. It uses the function of TextKit to draw characters.

let myLable : SwiftyLabel = SwiftyLabel("Label", .white, .blue).addTo(view)
myLable.frame = CGRect.init(x: 50, y: 300 + 20 + 20, width: 100, height: 100)
Swift

SwiftyButton

SwiftyButton is a better performance than UIButton and can be used like a standard UI component. Also, Easier to use than UIButton because of block-package and mistake double tap IgnoreEvent. Since UIControl is inherited instead of UIbutton, there is little wasteful processing. It uses the function of TextKit to draw characters and Image feature from GPU.

let myBtn : SwiftyButton = SwiftyButton("Button", myImage, ClosureWrapper({ [weak self] (btn) in
            
            guard let strongSelf = self, let btn = btn else { return }
            // do something
                                                                                
})).addTo(view)
myBtn.frame = CGRect(x: 50, y: 450 + 20 + 20 + 20, width: 100, height: 100)
Swift

SwiftyTimer

SwiftyTimer is running on RunLoop.

Timer.every(1.0, ClosureWrapper({ (timer) in
    print("Timer_every")
})).start()

Timer.after(5.0, ClosureWrapper({ (timer) in
    print("Timer_after")
})).start()
Swift

SwiftyToast

SwiftyToas is depend on global runloop cente, also show the toast without overlap.

SwiftyToast.load("This is a Toast")
Swift

SwiftyAlert

SwiftyAlert contains SuccessAlert, ErrorAlert, WarningAlert, InfoAlert, EditAlert and their special styles.

let alert: SwiftyAlertView = .create()

_ = alert.addTextField()

_ = alert.addButton("First Button", action: {
    print("First Button tapped")
})
_ = alert.addButton("Second Button") {
    print("Second button tapped")
}

let theAlert: SwiftyAlertViewResponder = alert.showSuccess("Congratulations", subTitle: "You've just displayed this awesome Pop Up View") //showError, showWarning, showInfo, showEdit
theAlert.setDismissBlock {
    print("Alert Dismissed")
}
Swift

SwiftyThreadPool

ThreadPool is used to manage threads which depends on active CPUs, also autorelease Runloop inside.

let myOperation : BlockOperation = .init {

    print("task2----Thread:\(Thread.current)")
    for i in 1...3
    {
        print("Task-------\(i)")
    }
}

ThreadPool.defalut.add(myOperation)
Swift

SwiftyPromise

Everyone knows PromiseKit and its story. I also use this library in my code. But it is too heavy for my code, so I build a lightweight version of PromiseKit, based partially on Javascript's A+ spec, depends on ThreadPool.

If you dont need send value from different threads in a Premise, it will be simple:

Promise<Void>.firstly(with: nil, on: .background) {

    print("Promise<Void>---task1----Thread:\(Thread.current)")

    }.then(on: .main) {

        print("Promise<Void>---task2----Thread:\(Thread.current)")
        throw SimpleError()

    }.then {

        print("Promise<Void>---task3----Thread:\(Thread.current)")
    }.always {

        print("Promise<Void>---taskAlways----Thread:\(Thread.current)")

    }.catch { (error) in

        print("Promise<Void>---error\(String(describing: error))")
}
Swift

Also you need to share or send value in different threads in a Promise, you should code as below:

Promise<String>.firstly(on: .background) { (update, _) in

    print("task1----Thread:\(Thread.current)")
    update("abc")

    }.then { (update, str) in

        print("thenthenthenthenthenthen----\(String(describing: str))") // abc
        var str = str
        str?.append("aaaaaaaa") // aaaaaaaaabc
        update(str)

    }.then(with: nil, on: .main) { (_, str) in

        print("mainmainmainmainmainmainmain----\(String(describing: str))") // aaaaaaaaabc
    }.catch()
Swift

To Do List

  • CameraKit, it is a lightweight camera framework.
  • OpenGL Video / Image processing.
  • Metal Video / Image processing.
  • OpenCV Computer vision processing.
  • Vision Framework Face Detection.

License

SwiftyUI is released under the MIT license. See LICENSE for details.

GitHub

https://github.com/haoking/SwiftyUI