initial commit

This commit is contained in:
Yonat Sharon 2016-11-28 16:55:00 +02:00
commit 44a6cef275
13 changed files with 825 additions and 0 deletions

1
.swift-version Normal file
View File

@ -0,0 +1 @@
3.0.1

32
BatteryView.podspec Normal file
View File

@ -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

View File

@ -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 = "<group>"; };
DCBCBEF61DEC700D00844FCB /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
DCBCBEF81DEC700D00844FCB /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
DCBCBEFB1DEC700D00844FCB /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
DCBCBEFD1DEC700D00844FCB /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
DCBCBF031DEC70DD00844FCB /* BatteryView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = BatteryView.swift; path = ../Sources/BatteryView.swift; sourceTree = "<group>"; };
/* 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 = "<group>";
};
DCBCBEEF1DEC700D00844FCB /* Products */ = {
isa = PBXGroup;
children = (
DCBCBEEE1DEC700D00844FCB /* BatteryViewDemo.app */,
);
name = Products;
sourceTree = "<group>";
};
DCBCBEF01DEC700D00844FCB /* BatteryViewDemo */ = {
isa = PBXGroup;
children = (
DCBCBEF11DEC700D00844FCB /* AppDelegate.swift */,
DCBCBEF51DEC700D00844FCB /* Main.storyboard */,
DCBCBEF81DEC700D00844FCB /* Assets.xcassets */,
DCBCBEFA1DEC700D00844FCB /* LaunchScreen.storyboard */,
DCBCBEFD1DEC700D00844FCB /* Info.plist */,
);
path = BatteryViewDemo;
sourceTree = "<group>";
};
/* 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 = "<group>";
};
DCBCBEFA1DEC700D00844FCB /* LaunchScreen.storyboard */ = {
isa = PBXVariantGroup;
children = (
DCBCBEFB1DEC700D00844FCB /* Base */,
);
name = LaunchScreen.storyboard;
sourceTree = "<group>";
};
/* 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 */;
}

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<Workspace
version = "1.0">
<FileRef
location = "self:BatteryViewDemo.xcodeproj">
</FileRef>
</Workspace>

View File

@ -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:.
}
}

View File

@ -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"
}
}

View File

@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="11134" systemVersion="15F34" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="01J-lp-oVM">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11106"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--View Controller-->
<scene sceneID="EHf-IW-A2E">
<objects>
<viewController id="01J-lp-oVM" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="Llm-lL-Icb"/>
<viewControllerLayoutGuide type="bottom" id="xb3-aO-Qok"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="Ze5-6b-2t3">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</view>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="iYj-Kq-Ea1" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="53" y="375"/>
</scene>
</scenes>
</document>

View File

@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="11542" systemVersion="15G1108" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11524"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--Battery View Controller-->
<scene sceneID="tne-QT-ifu">
<objects>
<viewController id="BYZ-38-t0r" customClass="BatteryViewController" customModule="BatteryViewDemo" customModuleProvider="target" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="y3c-jy-aDJ"/>
<viewControllerLayoutGuide type="bottom" id="wfy-db-euE"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="iEh-y5-b3Q" customClass="BatteryView" customModule="BatteryViewDemo" customModuleProvider="target">
<rect key="frame" x="137.5" y="607" width="100" height="40"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<constraints>
<constraint firstAttribute="height" constant="40" id="bFd-1R-A7K"/>
<constraint firstAttribute="width" constant="100" id="te7-85-axW"/>
</constraints>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="boolean" keyPath="isVertical" value="NO"/>
<userDefinedRuntimeAttribute type="number" keyPath="level">
<integer key="value" value="40"/>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
</view>
</subviews>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstItem="wfy-db-euE" firstAttribute="top" secondItem="iEh-y5-b3Q" secondAttribute="bottom" constant="20" id="Chp-Dd-o8T"/>
<constraint firstItem="iEh-y5-b3Q" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="zui-qd-LGW"/>
</constraints>
</view>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="32.799999999999997" y="38.23088455772114"/>
</scene>
</scenes>
</document>

View File

@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIMainStoryboardFile</key>
<string>Main</string>
<key>UIRequiredDeviceCapabilities</key>
<array>
<string>armv7</string>
</array>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
</dict>
</plist>

20
LICENSE.txt Normal file
View File

@ -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.

50
README.md Normal file
View File

@ -0,0 +1,50 @@
# StepProgressView
Simple battery shaped UIView.
<p align="center">
<img src="Screenshots/Battery.png">
</p>
## 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.

BIN
Screenshots/Battery.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

142
Sources/BatteryView.swift Normal file
View File

@ -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
}
}