Skip to content

Commit

Permalink
great progress: bug fix on presentViewController. add text notice on …
Browse files Browse the repository at this point in the history
…StatusBar.
  • Loading branch information
johnlui committed May 15, 2015
1 parent d1d1f96 commit d1780b5
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 35 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ SwiftNotice is a GUI library for displaying various popups written in pure Swift

##Features

![SwiftNotice gif](http://staticonsae.sinaapp.com/images/SwiftNotice2.gif)
![SwiftNotice gif](http://staticonsae.sinaapp.com/images/SwiftNotice3.gif)

##Pretty easy to use

Expand All @@ -13,6 +13,8 @@ In any subclass of UIViewController:
```swift
self.pleaseWait()

self.noticeTop("转发成功!")

self.successNotice("Success!")
self.errorNotice("Error!")
self.infoNotice("Info")
Expand All @@ -31,8 +33,7 @@ Just clone and add `SwiftNotice.swift` to your project.
##Requirements

* iOS 7.0+
* Xcode 6.3
* Swift 1.2
* Xcode 6.3 (Swift 1.2)

##Contribution

Expand Down
76 changes: 55 additions & 21 deletions SwiftNotice.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ import Foundation
import UIKit

extension UIViewController {
func noticeTop(text: String) {
SwiftNotice.noticeOnSatusBar(text, autoClear: true)
}
func noticeTop(text: String, autoClear: Bool) {
SwiftNotice.noticeOnSatusBar(text, autoClear: autoClear)
}
func successNotice(text: String) {
SwiftNotice.showNoticeWithText(NoticeType.success, text: text, autoClear: true)
}
Expand Down Expand Up @@ -50,17 +56,40 @@ enum NoticeType{

class SwiftNotice: NSObject {

static var mainViews = Array<UIView>()
static var windows = Array<UIWindow!>()
static let rv = UIApplication.sharedApplication().keyWindow?.subviews.first as! UIView

static func clear() {
for i in mainViews {
i.removeFromSuperview()
}
windows.removeAll(keepCapacity: false)
}

static func noticeOnSatusBar(text: String, autoClear: Bool) {
let frame = UIApplication.sharedApplication().statusBarFrame
let window = UIWindow(frame: frame)
let view = UIView(frame: frame)
view.backgroundColor = UIColor(red: 0x6a/0x100, green: 0xb4/0x100, blue: 0x9f/0x100, alpha: 1)

let label = UILabel(frame: frame)
label.textAlignment = NSTextAlignment.Center
label.font = UIFont.systemFontOfSize(12)
label.textColor = UIColor.whiteColor()
label.text = text
view.addSubview(label)

window.windowLevel = UIWindowLevelStatusBar
window.hidden = false
window.addSubview(view)
windows.append(window)

if autoClear {
let selector = Selector("hideNotice:")
self.performSelector(selector, withObject: view, afterDelay: 1)
}
}
static func wait() {
let mainView = UIView(frame: CGRectMake(0, 0, 78, 78))
let frame = CGRectMake(0, 0, 78, 78)
let window = UIWindow(frame: frame)
let mainView = UIView(frame: frame)
mainView.layer.cornerRadius = 12
mainView.backgroundColor = UIColor(red:0, green:0, blue:0, alpha: 0.8)

Expand All @@ -69,14 +98,15 @@ class SwiftNotice: NSObject {
ai.startAnimating()
mainView.addSubview(ai)

mainView.center = rv.center
rv.addSubview(mainView)

mainViews.append(mainView)
window.windowLevel = UIWindowLevelAlert
window.center = rv.center
window.hidden = false
window.addSubview(mainView)
windows.append(window)
}

static func showText(text: String) {
let frame = CGRectMake(0, 0, 200, 60)
let window = UIWindow(frame: frame)
let mainView = UIView(frame: frame)
mainView.layer.cornerRadius = 12
mainView.backgroundColor = UIColor(red:0, green:0, blue:0, alpha: 0.8)
Expand All @@ -88,14 +118,17 @@ class SwiftNotice: NSObject {
label.textColor = UIColor.whiteColor()
mainView.addSubview(label)

mainView.center = rv.center
rv.addSubview(mainView)

mainViews.append(mainView)
window.windowLevel = UIWindowLevelAlert
window.center = rv.center
window.hidden = false
window.addSubview(mainView)
windows.append(window)
}

static func showNoticeWithText(type: NoticeType,text: String, autoClear: Bool) {
var mainView = UIView(frame: CGRectMake(0, 0, 90, 90))
let frame = CGRectMake(0, 0, 90, 90)
let window = UIWindow(frame: frame)
let mainView = UIView(frame: frame)
mainView.layer.cornerRadius = 10
mainView.backgroundColor = UIColor(red:0, green:0, blue:0, alpha: 0.7)

Expand All @@ -120,12 +153,13 @@ class SwiftNotice: NSObject {
label.text = text
label.textAlignment = NSTextAlignment.Center
mainView.addSubview(label)

mainView.center = rv.center
rv.addSubview(mainView)

mainViews.append(mainView)


window.windowLevel = UIWindowLevelAlert
window.center = rv.center
window.hidden = false
window.addSubview(mainView)
windows.append(window)

if autoClear {
let selector = Selector("hideNotice:")
self.performSelector(selector, withObject: mainView, afterDelay: 3)
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<Bucket
type = "1"
version = "2.0">
</Bucket>
34 changes: 23 additions & 11 deletions SwiftNoticeExample/SwiftNoticeExample/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="7531" systemVersion="14D136" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="vXZ-lx-hvc">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="7702" systemVersion="14D136" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="vXZ-lx-hvc">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="7520"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="7701"/>
</dependencies>
<scenes>
<!--View Controller-->
Expand All @@ -16,38 +16,38 @@
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="DSY-kU-1Hg">
<rect key="frame" x="222" y="116" width="157" height="41"/>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="DSY-kU-1Hg" userLabel="successNotice">
<rect key="frame" x="223" y="116" width="155" height="41"/>
<fontDescription key="fontDescription" type="system" pointSize="24"/>
<state key="normal" title="notice success">
<state key="normal" title="successNotice">
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
</state>
<connections>
<action selector="noticeSuccess:" destination="vXZ-lx-hvc" eventType="touchUpInside" id="2aT-ne-wOy"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="fZy-0c-TYE">
<rect key="frame" x="222" y="165" width="157" height="41"/>
<rect key="frame" x="223" y="165" width="155" height="41"/>
<fontDescription key="fontDescription" type="system" pointSize="24"/>
<state key="normal" title="notice error">
<state key="normal" title="errorNotice">
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
</state>
<connections>
<action selector="noticeError:" destination="vXZ-lx-hvc" eventType="touchUpInside" id="ruD-Jy-tXn"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="uhX-xe-SUb">
<rect key="frame" x="222" y="214" width="157" height="41"/>
<rect key="frame" x="223" y="214" width="155" height="41"/>
<fontDescription key="fontDescription" type="system" pointSize="24"/>
<state key="normal" title="notice info">
<state key="normal" title="infoNotice">
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
</state>
<connections>
<action selector="noticeInfo:" destination="vXZ-lx-hvc" eventType="touchUpInside" id="PXD-xy-XH6"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="Kzm-el-j3t">
<rect key="frame" x="222" y="539" width="157" height="41"/>
<rect key="frame" x="223" y="539" width="155" height="41"/>
<fontDescription key="fontDescription" type="system" pointSize="24"/>
<state key="normal" title="clearAllNotice">
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
Expand All @@ -57,7 +57,7 @@
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="MEE-N6-q62">
<rect key="frame" x="222" y="263" width="157" height="41"/>
<rect key="frame" x="223" y="263" width="155" height="41"/>
<fontDescription key="fontDescription" type="system" pointSize="24"/>
<state key="normal" title="noticeOnlyText">
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
Expand All @@ -76,6 +76,16 @@
<action selector="wait:" destination="vXZ-lx-hvc" eventType="touchUpInside" id="aPu-0P-t4c"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="yTy-gc-doX">
<rect key="frame" x="249" y="344" width="103" height="41"/>
<fontDescription key="fontDescription" type="system" pointSize="24"/>
<state key="normal" title="noticeTop">
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
</state>
<connections>
<action selector="topNotice:" destination="vXZ-lx-hvc" eventType="touchUpInside" id="Zpm-pn-2Ze"/>
</connections>
</button>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
<constraints>
Expand All @@ -87,6 +97,8 @@
<constraint firstItem="fZy-0c-TYE" firstAttribute="trailing" secondItem="DSY-kU-1Hg" secondAttribute="trailing" id="Co5-KC-GgM"/>
<constraint firstItem="MEE-N6-q62" firstAttribute="top" secondItem="uhX-xe-SUb" secondAttribute="bottom" constant="8" symbolic="YES" id="E6R-2z-0wt"/>
<constraint firstItem="VX1-bL-S67" firstAttribute="top" secondItem="jyV-Pf-zRb" secondAttribute="bottom" constant="47" id="MNc-WO-j1d"/>
<constraint firstItem="yTy-gc-doX" firstAttribute="top" secondItem="MEE-N6-q62" secondAttribute="bottom" constant="40" id="Obb-2w-KNG"/>
<constraint firstItem="yTy-gc-doX" firstAttribute="centerX" secondItem="MEE-N6-q62" secondAttribute="centerX" id="Tb6-ug-hp7"/>
<constraint firstItem="Kzm-el-j3t" firstAttribute="trailing" secondItem="MEE-N6-q62" secondAttribute="trailing" id="Tvq-0a-NBJ"/>
<constraint firstItem="VX1-bL-S67" firstAttribute="centerX" secondItem="kh9-bI-dsS" secondAttribute="centerX" id="Zye-xI-hol"/>
<constraint firstItem="MEE-N6-q62" firstAttribute="trailing" secondItem="uhX-xe-SUb" secondAttribute="trailing" id="aEk-Ez-DZQ"/>
Expand Down
3 changes: 3 additions & 0 deletions SwiftNoticeExample/SwiftNoticeExample/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class ViewController: UIViewController {
// Dispose of any resources that can be recreated.
}

@IBAction func topNotice(sender: AnyObject) {
self.noticeTop("转发成功!")
}
@IBAction func wait(sender: AnyObject) {
self.pleaseWait()
}
Expand Down

0 comments on commit d1780b5

Please sign in to comment.