From 44a6cef275f1f0dca8d3d23ff3d38161baf0db71 Mon Sep 17 00:00:00 2001 From: Yonat Sharon Date: Mon, 28 Nov 2016 16:55:00 +0200 Subject: [PATCH] initial commit --- .swift-version | 1 + BatteryView.podspec | 32 ++ .../BatteryViewDemo.xcodeproj/project.pbxproj | 307 ++++++++++++++++++ .../contents.xcworkspacedata | 7 + Example/BatteryViewDemo/AppDelegate.swift | 75 +++++ .../AppIcon.appiconset/Contents.json | 68 ++++ .../Base.lproj/LaunchScreen.storyboard | 27 ++ .../Base.lproj/Main.storyboard | 51 +++ Example/BatteryViewDemo/Info.plist | 45 +++ LICENSE.txt | 20 ++ README.md | 50 +++ Screenshots/Battery.png | Bin 0 -> 16391 bytes Sources/BatteryView.swift | 142 ++++++++ 13 files changed, 825 insertions(+) create mode 100644 .swift-version create mode 100644 BatteryView.podspec create mode 100644 Example/BatteryViewDemo.xcodeproj/project.pbxproj create mode 100644 Example/BatteryViewDemo.xcodeproj/project.xcworkspace/contents.xcworkspacedata create mode 100644 Example/BatteryViewDemo/AppDelegate.swift create mode 100644 Example/BatteryViewDemo/Assets.xcassets/AppIcon.appiconset/Contents.json create mode 100644 Example/BatteryViewDemo/Base.lproj/LaunchScreen.storyboard create mode 100644 Example/BatteryViewDemo/Base.lproj/Main.storyboard create mode 100644 Example/BatteryViewDemo/Info.plist create mode 100644 LICENSE.txt create mode 100644 README.md create mode 100644 Screenshots/Battery.png create mode 100644 Sources/BatteryView.swift diff --git a/.swift-version b/.swift-version new file mode 100644 index 0000000..cb2b00e --- /dev/null +++ b/.swift-version @@ -0,0 +1 @@ +3.0.1 diff --git a/BatteryView.podspec b/BatteryView.podspec new file mode 100644 index 0000000..485dee4 --- /dev/null +++ b/BatteryView.podspec @@ -0,0 +1,32 @@ + +Pod::Spec.new do |s| + + s.name = "BatteryView" + s.version = "1.0.0" + s.summary = "Simple battery shaped UIView." + + s.description = <<-DESC +Usage: + +```swift +let batteryView = BatteryView(frame: smallRect) +batteryView.level = 42 // anywhere in 0...100 +``` + DESC + + s.homepage = "https://github.com/yonat/BatteryView" + s.screenshots = "https://raw.githubusercontent.com/yonat/BatteryView/master/Screenshots/Battery.png" + s.license = { :type => "MIT", :file => "LICENSE.txt" } + + s.author = { "Yonat Sharon" => "yonat@ootips.org" } + s.social_media_url = "http://twitter.com/yonatsharon" + + s.platform = :ios, "8.0" + + s.source = { :git => "https://github.com/yonat/BatteryView.git", :tag => s.version } + + s.source_files = "Sources/*" + + s.requires_arc = true + +end diff --git a/Example/BatteryViewDemo.xcodeproj/project.pbxproj b/Example/BatteryViewDemo.xcodeproj/project.pbxproj new file mode 100644 index 0000000..02124e9 --- /dev/null +++ b/Example/BatteryViewDemo.xcodeproj/project.pbxproj @@ -0,0 +1,307 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 46; + objects = { + +/* Begin PBXBuildFile section */ + DCBCBEF21DEC700D00844FCB /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCBCBEF11DEC700D00844FCB /* AppDelegate.swift */; }; + DCBCBEF71DEC700D00844FCB /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = DCBCBEF51DEC700D00844FCB /* Main.storyboard */; }; + DCBCBEF91DEC700D00844FCB /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = DCBCBEF81DEC700D00844FCB /* Assets.xcassets */; }; + DCBCBEFC1DEC700D00844FCB /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = DCBCBEFA1DEC700D00844FCB /* LaunchScreen.storyboard */; }; + DCBCBF041DEC70DD00844FCB /* BatteryView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCBCBF031DEC70DD00844FCB /* BatteryView.swift */; }; +/* End PBXBuildFile section */ + +/* Begin PBXFileReference section */ + DCBCBEEE1DEC700D00844FCB /* BatteryViewDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = BatteryViewDemo.app; sourceTree = BUILT_PRODUCTS_DIR; }; + DCBCBEF11DEC700D00844FCB /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; + DCBCBEF61DEC700D00844FCB /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; + DCBCBEF81DEC700D00844FCB /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; + DCBCBEFB1DEC700D00844FCB /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; + DCBCBEFD1DEC700D00844FCB /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + DCBCBF031DEC70DD00844FCB /* BatteryView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = BatteryView.swift; path = ../Sources/BatteryView.swift; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + DCBCBEEB1DEC700D00844FCB /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + DCBCBEE51DEC700D00844FCB = { + isa = PBXGroup; + children = ( + DCBCBF031DEC70DD00844FCB /* BatteryView.swift */, + DCBCBEF01DEC700D00844FCB /* BatteryViewDemo */, + DCBCBEEF1DEC700D00844FCB /* Products */, + ); + sourceTree = ""; + }; + DCBCBEEF1DEC700D00844FCB /* Products */ = { + isa = PBXGroup; + children = ( + DCBCBEEE1DEC700D00844FCB /* BatteryViewDemo.app */, + ); + name = Products; + sourceTree = ""; + }; + DCBCBEF01DEC700D00844FCB /* BatteryViewDemo */ = { + isa = PBXGroup; + children = ( + DCBCBEF11DEC700D00844FCB /* AppDelegate.swift */, + DCBCBEF51DEC700D00844FCB /* Main.storyboard */, + DCBCBEF81DEC700D00844FCB /* Assets.xcassets */, + DCBCBEFA1DEC700D00844FCB /* LaunchScreen.storyboard */, + DCBCBEFD1DEC700D00844FCB /* Info.plist */, + ); + path = BatteryViewDemo; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + DCBCBEED1DEC700D00844FCB /* BatteryViewDemo */ = { + isa = PBXNativeTarget; + buildConfigurationList = DCBCBF001DEC700D00844FCB /* Build configuration list for PBXNativeTarget "BatteryViewDemo" */; + buildPhases = ( + DCBCBEEA1DEC700D00844FCB /* Sources */, + DCBCBEEB1DEC700D00844FCB /* Frameworks */, + DCBCBEEC1DEC700D00844FCB /* Resources */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = BatteryViewDemo; + productName = BatteryViewDemo; + productReference = DCBCBEEE1DEC700D00844FCB /* BatteryViewDemo.app */; + productType = "com.apple.product-type.application"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + DCBCBEE61DEC700D00844FCB /* Project object */ = { + isa = PBXProject; + attributes = { + LastSwiftUpdateCheck = 0810; + LastUpgradeCheck = 0810; + ORGANIZATIONNAME = "Yonat Sharon"; + TargetAttributes = { + DCBCBEED1DEC700D00844FCB = { + CreatedOnToolsVersion = 8.1; + ProvisioningStyle = Automatic; + }; + }; + }; + buildConfigurationList = DCBCBEE91DEC700D00844FCB /* Build configuration list for PBXProject "BatteryViewDemo" */; + compatibilityVersion = "Xcode 3.2"; + developmentRegion = English; + hasScannedForEncodings = 0; + knownRegions = ( + en, + Base, + ); + mainGroup = DCBCBEE51DEC700D00844FCB; + productRefGroup = DCBCBEEF1DEC700D00844FCB /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + DCBCBEED1DEC700D00844FCB /* BatteryViewDemo */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXResourcesBuildPhase section */ + DCBCBEEC1DEC700D00844FCB /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + DCBCBEFC1DEC700D00844FCB /* LaunchScreen.storyboard in Resources */, + DCBCBEF91DEC700D00844FCB /* Assets.xcassets in Resources */, + DCBCBEF71DEC700D00844FCB /* Main.storyboard in Resources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXResourcesBuildPhase section */ + +/* Begin PBXSourcesBuildPhase section */ + DCBCBEEA1DEC700D00844FCB /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + DCBCBF041DEC70DD00844FCB /* BatteryView.swift in Sources */, + DCBCBEF21DEC700D00844FCB /* AppDelegate.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin PBXVariantGroup section */ + DCBCBEF51DEC700D00844FCB /* Main.storyboard */ = { + isa = PBXVariantGroup; + children = ( + DCBCBEF61DEC700D00844FCB /* Base */, + ); + name = Main.storyboard; + sourceTree = ""; + }; + DCBCBEFA1DEC700D00844FCB /* LaunchScreen.storyboard */ = { + isa = PBXVariantGroup; + children = ( + DCBCBEFB1DEC700D00844FCB /* Base */, + ); + name = LaunchScreen.storyboard; + sourceTree = ""; + }; +/* End PBXVariantGroup section */ + +/* Begin XCBuildConfiguration section */ + DCBCBEFE1DEC700D00844FCB /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_SUSPICIOUS_MOVES = YES; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 10.1; + MTL_ENABLE_DEBUG_INFO = YES; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = iphoneos; + SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + TARGETED_DEVICE_FAMILY = "1,2"; + }; + name = Debug; + }; + DCBCBEFF1DEC700D00844FCB /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_SUSPICIOUS_MOVES = YES; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 10.1; + MTL_ENABLE_DEBUG_INFO = NO; + SDKROOT = iphoneos; + SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; + TARGETED_DEVICE_FAMILY = "1,2"; + VALIDATE_PRODUCT = YES; + }; + name = Release; + }; + DCBCBF011DEC700D00844FCB /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + INFOPLIST_FILE = BatteryViewDemo/Info.plist; + LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = com.roysharon.BatteryViewDemo; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; + }; + name = Debug; + }; + DCBCBF021DEC700D00844FCB /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + INFOPLIST_FILE = BatteryViewDemo/Info.plist; + LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = com.roysharon.BatteryViewDemo; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + DCBCBEE91DEC700D00844FCB /* Build configuration list for PBXProject "BatteryViewDemo" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + DCBCBEFE1DEC700D00844FCB /* Debug */, + DCBCBEFF1DEC700D00844FCB /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + DCBCBF001DEC700D00844FCB /* Build configuration list for PBXNativeTarget "BatteryViewDemo" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + DCBCBF011DEC700D00844FCB /* Debug */, + DCBCBF021DEC700D00844FCB /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = DCBCBEE61DEC700D00844FCB /* Project object */; +} diff --git a/Example/BatteryViewDemo.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/Example/BatteryViewDemo.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..78deb61 --- /dev/null +++ b/Example/BatteryViewDemo.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/Example/BatteryViewDemo/AppDelegate.swift b/Example/BatteryViewDemo/AppDelegate.swift new file mode 100644 index 0000000..37e4913 --- /dev/null +++ b/Example/BatteryViewDemo/AppDelegate.swift @@ -0,0 +1,75 @@ +// +// AppDelegate.swift +// BatteryViewDemo +// +// Created by Yonat Sharon on 28.11.2016. +// Copyright © 2016 Yonat Sharon. All rights reserved. +// + +import UIKit + +class BatteryViewController: UIViewController { + + override func viewDidLoad() { + super.viewDidLoad() + let bigBattery = BatteryView(frame: view.bounds.insetBy(dx: 60, dy: 120)) + view.addSubview(bigBattery) + + let littleBattery = BatteryView(frame: CGRect(x: 50, y: 30, width: 25, height: 45)) + littleBattery.lowThreshold = 20 + view.addSubview(littleBattery) + + let horizontalBettery = BatteryView(frame: CGRect(x: 140, y: 30, width: 140, height: 60)) + horizontalBettery.direction = .minXEdge + horizontalBettery.lowThreshold = 30 + horizontalBettery.borderWidth = 3 + horizontalBettery.highLevelColor = .purple + horizontalBettery.lowLevelColor = .magenta + horizontalBettery.backgroundColor = .cyan + horizontalBettery.cornerRadius = 10 + view.addSubview(horizontalBettery) + + for i in 0...10 { + DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(i)) { + [bigBattery, littleBattery, horizontalBettery].forEach {$0.level = 100 - 10 * i} + } + } + } +} + +@UIApplicationMain +class AppDelegate: UIResponder, UIApplicationDelegate { + + var window: UIWindow? + + + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { + // Override point for customization after application launch. + return true + } + + func applicationWillResignActive(_ application: UIApplication) { + // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. + // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. + } + + func applicationDidEnterBackground(_ application: UIApplication) { + // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. + // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. + } + + func applicationWillEnterForeground(_ application: UIApplication) { + // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. + } + + func applicationDidBecomeActive(_ application: UIApplication) { + // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. + } + + func applicationWillTerminate(_ application: UIApplication) { + // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. + } + + +} + diff --git a/Example/BatteryViewDemo/Assets.xcassets/AppIcon.appiconset/Contents.json b/Example/BatteryViewDemo/Assets.xcassets/AppIcon.appiconset/Contents.json new file mode 100644 index 0000000..36d2c80 --- /dev/null +++ b/Example/BatteryViewDemo/Assets.xcassets/AppIcon.appiconset/Contents.json @@ -0,0 +1,68 @@ +{ + "images" : [ + { + "idiom" : "iphone", + "size" : "29x29", + "scale" : "2x" + }, + { + "idiom" : "iphone", + "size" : "29x29", + "scale" : "3x" + }, + { + "idiom" : "iphone", + "size" : "40x40", + "scale" : "2x" + }, + { + "idiom" : "iphone", + "size" : "40x40", + "scale" : "3x" + }, + { + "idiom" : "iphone", + "size" : "60x60", + "scale" : "2x" + }, + { + "idiom" : "iphone", + "size" : "60x60", + "scale" : "3x" + }, + { + "idiom" : "ipad", + "size" : "29x29", + "scale" : "1x" + }, + { + "idiom" : "ipad", + "size" : "29x29", + "scale" : "2x" + }, + { + "idiom" : "ipad", + "size" : "40x40", + "scale" : "1x" + }, + { + "idiom" : "ipad", + "size" : "40x40", + "scale" : "2x" + }, + { + "idiom" : "ipad", + "size" : "76x76", + "scale" : "1x" + }, + { + "idiom" : "ipad", + "size" : "76x76", + "scale" : "2x" + } + ], + "info" : { + "version" : 1, + "author" : "xcode" + } +} \ No newline at end of file diff --git a/Example/BatteryViewDemo/Base.lproj/LaunchScreen.storyboard b/Example/BatteryViewDemo/Base.lproj/LaunchScreen.storyboard new file mode 100644 index 0000000..fdf3f97 --- /dev/null +++ b/Example/BatteryViewDemo/Base.lproj/LaunchScreen.storyboard @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Example/BatteryViewDemo/Base.lproj/Main.storyboard b/Example/BatteryViewDemo/Base.lproj/Main.storyboard new file mode 100644 index 0000000..1bcc91d --- /dev/null +++ b/Example/BatteryViewDemo/Base.lproj/Main.storyboard @@ -0,0 +1,51 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Example/BatteryViewDemo/Info.plist b/Example/BatteryViewDemo/Info.plist new file mode 100644 index 0000000..d052473 --- /dev/null +++ b/Example/BatteryViewDemo/Info.plist @@ -0,0 +1,45 @@ + + + + + CFBundleDevelopmentRegion + en + CFBundleExecutable + $(EXECUTABLE_NAME) + CFBundleIdentifier + $(PRODUCT_BUNDLE_IDENTIFIER) + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + $(PRODUCT_NAME) + CFBundlePackageType + APPL + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1 + LSRequiresIPhoneOS + + UILaunchStoryboardName + LaunchScreen + UIMainStoryboardFile + Main + UIRequiredDeviceCapabilities + + armv7 + + UISupportedInterfaceOrientations + + UIInterfaceOrientationPortrait + UIInterfaceOrientationLandscapeLeft + UIInterfaceOrientationLandscapeRight + + UISupportedInterfaceOrientations~ipad + + UIInterfaceOrientationPortrait + UIInterfaceOrientationPortraitUpsideDown + UIInterfaceOrientationLandscapeLeft + UIInterfaceOrientationLandscapeRight + + + diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..dd76773 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,20 @@ +The MIT License (MIT) + +Copyright (c) 2015 Yonat Sharon + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..0985bf3 --- /dev/null +++ b/README.md @@ -0,0 +1,50 @@ +# StepProgressView +Simple battery shaped UIView. + +

+ +

+ +## Usage + +```swift +let batteryView = BatteryView(frame: smallRect) +batteryView.level = 42 // anywhere in 0...100 +batteryView.lowThreshold = 25 // battery fill becomes red if level is below this threshold +``` + +## Changing Appearance + +**Fill Colors:** + + +```swift +batteryView.highLevelColor = .green +batteryView.lowLevelColor = .red +batteryView.noLevelColor = .gray +``` + +**Battery Shape:** + +```swift +batteryView.direction = .minXEdge // terminal facing left + +batteryView.terminalLengthRatio = 0.1 // relative to battery length +batteryView.terminalWidthRatio = 0.4 // relative to battery width + +batteryView.borderWidth = 2.5 // default is batteryLength / 20 +batteryView.cornerRadius = 5 // default is batteryLength / 10 + +``` + +## Installation + +### CocoaPods: + +```ruby +pod 'BatteryView' +``` + +### Manually: + +Copy `Sources/*` to your Xcode project. diff --git a/Screenshots/Battery.png b/Screenshots/Battery.png new file mode 100644 index 0000000000000000000000000000000000000000..7421fa072d72cfa3a7bc6dbe52bbfc8a7d7834ba GIT binary patch literal 16391 zcmeHucT`hf*CxFxRRrl^0qGs-2qG#{lwLv)NDU?QDo9Z&N>wow1t}qfmVi{HDo7_l zfPhr#J@hgc{C)Gi^UlowGi$xB#WMGtv-c@??{l8}?0sJ9>1bZ2Vx=M=Ah`VS!M&#h z1cWodpFIT`P{P=xZvy;B;&@m4E&)MVBK5Hi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+1!Tl3DcS0JntN8OaE-3 z`0QlfZy^6+@~tQ%2e+^7+9mx=;^^q&CJXv+prn`ED&8I&?et+qP}Yd zE!v-IRa3Y0!}$oF9u?*(vZUAZ+J?E1>z>irf`R)EpB%4%IyTJo9_6@$95y{Hyn8r*;stUFslfc^a~M z`c=`)u^%tIGxnYfFJaZ1_cl_=z{n~DuV?IYYt;&;Z)llo8N(EE+A~_#afU~RDQ(a( zZ{~3NHgYyqE47){f8FX3gd;FZK0X?Esg>yMQoCx3QNhC?Kf`0|@f^bC(~L6=^_LyT=Lk%JJMyF?YPJBu;--6Yq>&%`3jz+8-Pw z&}Q^Zp>_xYy15SA+QJZ*{$4CH-CHP#MHt^3nX}7}k%y|>9`kSA0B&&Q~n8C0#=8{3#Iy zdG;?zRc&&}6Q6=IAepKfz>YW=aII)cOT6Qr)%Q)@@RS|nsgvs;NM+33+KdT8dLA(f z!n4fc0nHX5nRIaj%saWt+Aw7MP^9{I#0JPVB;Wk|Yu)86Qq1p0Wq6l(_>4 z*W26;mAVbW*q9qRUVX{lm)O*5BIL|g@okaO=rlA1=x}U&_GdDTYgwMx^?psmT5`GG`ubr{qgK{J?v~u>3E}gN=xtX zRowbn)$A`Gz4UM&@ZwNR7s(rni&?yp@i+MawBDCC7nyb$3$t42?pW_lg* z#08|}+1&=d2At}pj7lwj{7+#x9fdWaoeq@K)xMu|==c(Rg%xX>5wkix56~uC9c@mP78z*EQ+&U}3^;we1qGFXq@X7P zhnUp4$D&yew3nHjm(fGgk3PFyE%$WAZdKkfRjBMnJMNi6V0l%+t5sbU%PzoumaQK5 zipmeNW)M%;+IcE`hs{xe_P|0F0wUG3R^c0#);7K1=Zi&;1$MDv)3wt<-CrM{tRwyh Dpf{@# literal 0 HcmV?d00001 diff --git a/Sources/BatteryView.swift b/Sources/BatteryView.swift new file mode 100644 index 0000000..2c2d8d6 --- /dev/null +++ b/Sources/BatteryView.swift @@ -0,0 +1,142 @@ +// +// BatteryView.swift +// Show a battery oriented toward ''direction', charged ''level'' percent. +// Turns red when level drops below ''lowThreshold''. +// +// Created by Yonat Sharon on 6/1/15. +// Copyright (c) 2015-6 Yonat Sharon. All rights reserved. +// + +import UIKit + +@IBDesignable +open class BatteryView: UIView { + + // MARK: - Behavior Properties + + /// 0 to 100 percent full, unavailable = -1 + @IBInspectable open var level: Int = -1 { didSet {layoutLevel()} } + + /// change color when level crosses the threshold + @IBInspectable open var lowThreshold: Int = 10 { didSet {layoutFillColor()} } + + // MARK: - Appearance Properties + + /// direction of battery terminal + open var direction: CGRectEdge = .minYEdge { didSet {setNeedsLayout()} } + + /// simplified direction of battery terminal (for Interface Builder) + @IBInspectable open var isVertical: Bool { + get {return direction == .maxYEdge || direction == .minYEdge} + set {direction = newValue ? .minYEdge : .maxXEdge} + } + + // relative size of battery terminal + @IBInspectable open var terminalLengthRatio: CGFloat = 0.1 { didSet {setNeedsLayout()} } + @IBInspectable open var terminalWidthRatio: CGFloat = 0.4 { didSet {setNeedsLayout()} } + + @IBInspectable open var highLevelColor: UIColor = UIColor(red: 0.0, green: 0.9, blue: 0.0, alpha: 1) { didSet {layoutFillColor()} } + @IBInspectable open var lowLevelColor: UIColor = UIColor(red: 0.9, green: 0.0, blue: 0.0, alpha: 1) { didSet {layoutFillColor()} } + @IBInspectable open var noLevelColor: UIColor = UIColor(red: 0.8, green: 0.8, blue: 0.8, alpha: 1) { didSet {layoutFillColor()} } + + /// set as 0 for default borderWidth = length / 20 + @IBInspectable open var borderWidth: CGFloat = 0 { didSet {layoutBattery(); layoutLevel()} } + + /// set as 0 for default cornerRadius = length / 10 + @IBInspectable open var cornerRadius: CGFloat = 0 { didSet {layoutCornerRadius()} } + + // MARK: - Overrides + + override open var backgroundColor: UIColor? { didSet {layoutFillColor()} } + + override public init(frame: CGRect) { + super.init(frame: frame) + setUp() + } + + required public init?(coder: NSCoder) { + super.init(coder: coder) + setUp() + } + + override open func layoutSubviews() { + layoutBattery() + layoutLevel() + } + + // MARK: - Sublayers + + private var bodyOutline = CALayer() + private var terminalOutline = CALayer() + private var terminalOpening = CALayer() + private var levelFill = CALayer() + + private func setUp() { + layer.addSublayer(levelFill) + layer.addSublayer(bodyOutline) + layer.addSublayer(terminalOutline) + layer.addSublayer(terminalOpening) + setNeedsLayout() + } + + // MARK: - Layout + + private var length: CGFloat {return isVertical ? bounds.height : bounds.width} + + private func layoutBattery() { + // divide total length into body and terminal + let terminalLength = terminalLengthRatio * length + var (terminalFrame, bodyFrame) = bounds.divided(atDistance: terminalLength, from: direction) + + // layout body + bodyOutline.frame = bodyFrame + bodyOutline.borderWidth = borderWidth != 0 ? borderWidth : length / 20 + + // layout terminal + let parallelInsetRatio = (1-terminalWidthRatio) / 2 + let perpendicularInset = bodyOutline.borderWidth + var (dx, dy) = isVertical ? ( parallelInsetRatio * bounds.width, -perpendicularInset ) : ( -perpendicularInset, parallelInsetRatio * bounds.height ) + terminalFrame = terminalFrame.insetBy(dx: dx, dy: dy) + (_, terminalFrame) = terminalFrame.divided(atDistance: perpendicularInset, from: direction) + terminalOutline.frame = terminalFrame + terminalOutline.borderWidth = bodyOutline.borderWidth + + // cover terminal opening + var (_, coverFrame) = terminalFrame.divided(atDistance: perpendicularInset, from: direction) + (dx, dy) = isVertical ? (perpendicularInset, -0.25) : (-0.25, perpendicularInset) + coverFrame = coverFrame.insetBy(dx: dx, dy: dy) + terminalOpening.frame = coverFrame + terminalOpening.backgroundColor = noLevelColor.cgColor + + // layout empty levelFill + levelFill.frame = bodyFrame.insetBy(dx: perpendicularInset, dy: perpendicularInset).integral + levelFill.backgroundColor = noLevelColor.cgColor + } + + private func layoutLevel() { + var levelFrame = bodyOutline.frame.insetBy(dx: bodyOutline.borderWidth, dy: bodyOutline.borderWidth) + if level >= 0 && level <= 100 { + let levelInset = (isVertical ? levelFrame.height : levelFrame.width) * CGFloat(100-level) / 100 + (_, levelFrame) = levelFrame.divided(atDistance: levelInset, from: direction) + } + levelFill.frame = levelFrame.integral + layoutCornerRadius() + layoutFillColor() + } + + private func layoutFillColor() { + if level >= 0 && level <= 100 { + levelFill.backgroundColor = (level > lowThreshold ? highLevelColor : lowLevelColor).cgColor + terminalOpening.backgroundColor = (backgroundColor ?? .white).cgColor + } + else { + levelFill.backgroundColor = noLevelColor.cgColor + terminalOpening.backgroundColor = noLevelColor.cgColor + } + } + + private func layoutCornerRadius() { + bodyOutline.cornerRadius = cornerRadius != 0 ? cornerRadius : length / 10 + terminalOutline.cornerRadius = bodyOutline.cornerRadius / 2 + } +}