Skip to content

Commit

Permalink
第一天学习
Browse files Browse the repository at this point in the history
  • Loading branch information
qianyb committed Dec 17, 2018
1 parent a353afd commit 95259b3
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
TargetAttributes = {
9FEF1CF51C3A23BC00B2C64E = {
CreatedOnToolsVersion = 7.2;
DevelopmentTeam = F3YT8BWBYJ;
};
};
};
Expand Down Expand Up @@ -266,23 +267,27 @@
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
DEVELOPMENT_TEAM = F3YT8BWBYJ;
INFOPLIST_FILE = StopWatch/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 11.2;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = me.appkitchen.StopWatch;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 4.2;
};
name = Debug;
};
9FEF1D231C3A23BC00B2C64E /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
DEVELOPMENT_TEAM = F3YT8BWBYJ;
INFOPLIST_FILE = StopWatch/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 11.2;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = me.appkitchen.StopWatch;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 4.2;
};
name = Release;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?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>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
32 changes: 15 additions & 17 deletions Project 01 - SimpleStopWatch/StopWatch/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13771" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14460.31" 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="13772"/>
<capability name="Constraints to layout margins" minToolsVersion="6.0"/>
<capability name="Constraints with non-1.0 multipliers" minToolsVersion="5.1"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14460.20"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<customFonts key="customFonts">
<array key="AvenirNext.ttc">
<string>AvenirNext-UltraLight</string>
</array>
</customFonts>
<scenes>
<!--View Controller-->
<scene sceneID="tne-QT-ifu">
Expand All @@ -31,17 +24,20 @@
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="NrM-kS-GRc">
<rect key="frame" x="0.0" y="20" width="375" height="260"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="PgZ-tJ-umJ">
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="PgZ-tJ-umJ">
<rect key="frame" x="249" y="26" width="112" height="36"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<color key="backgroundColor" red="0.0" green="0.0" blue="0.0" alpha="0.0" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstAttribute="height" constant="36" id="XEW-mj-Bh8"/>
<constraint firstAttribute="width" constant="112" id="z4a-9n-d1h"/>
</constraints>
<fontDescription key="fontDescription" style="UICTFontTextStyleTitle1"/>
<inset key="titleEdgeInsets" minX="10" minY="0.0" maxX="10" maxY="0.0"/>
<state key="normal" title="Reset">
<color key="titleColor" red="0.99987119436264038" green="0.99998223781585693" blue="0.99984109401702881" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</state>
<connections>
<action selector="resetButtonDidTouch:" destination="BYZ-38-t0r" eventType="touchUpInside" id="rCT-x3-lAO"/>
<action selector="resetButtonDidTouch:" destination="BYZ-38-t0r" eventType="touchUpInside" id="byc-KY-3dh"/>
</connections>
</button>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="0" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="mHC-bT-4Ze">
Expand All @@ -58,6 +54,8 @@
<constraints>
<constraint firstItem="mHC-bT-4Ze" firstAttribute="centerX" secondItem="NrM-kS-GRc" secondAttribute="centerX" id="MDK-YB-54R"/>
<constraint firstItem="mHC-bT-4Ze" firstAttribute="centerY" secondItem="NrM-kS-GRc" secondAttribute="centerY" id="TpR-Wm-zll"/>
<constraint firstAttribute="trailing" secondItem="PgZ-tJ-umJ" secondAttribute="trailing" constant="14" id="amt-2g-8m0"/>
<constraint firstItem="PgZ-tJ-umJ" firstAttribute="top" secondItem="NrM-kS-GRc" secondAttribute="top" constant="26" id="fSy-VK-HNQ"/>
</constraints>
</view>
<button opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="249" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="LKO-Cn-wOv">
Expand All @@ -67,7 +65,7 @@
<color key="titleColor" red="0.99987119436264038" green="0.99998223781585693" blue="0.99984109401702881" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</state>
<connections>
<action selector="pauseButtonDidTouch:" destination="BYZ-38-t0r" eventType="touchUpInside" id="BFZ-77-jqL"/>
<action selector="pauseButtonDidTouch:" destination="BYZ-38-t0r" eventType="touchUpInside" id="jSp-cL-wgw"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="qLk-wD-lav">
Expand All @@ -77,7 +75,7 @@
<color key="titleColor" red="0.99987119436264038" green="0.99998223781585693" blue="0.99984109401702881" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</state>
<connections>
<action selector="playButtonDidTouch:" destination="BYZ-38-t0r" eventType="touchUpInside" id="Qdw-ZT-asN"/>
<action selector="playButtonDidTouch:" destination="BYZ-38-t0r" eventType="touchUpInside" id="xcg-hL-jb1"/>
</connections>
</button>
</subviews>
Expand All @@ -98,9 +96,9 @@
</constraints>
</view>
<connections>
<outlet property="pauseBtn" destination="LKO-Cn-wOv" id="zC6-zm-udv"/>
<outlet property="playBtn" destination="qLk-wD-lav" id="f5H-QW-lZB"/>
<outlet property="timeLabel" destination="mHC-bT-4Ze" id="yWs-Nc-IIZ"/>
<outlet property="pauseBtn" destination="LKO-Cn-wOv" id="wMs-Gr-SnB"/>
<outlet property="playBtn" destination="qLk-wD-lav" id="EBq-Hv-hHn"/>
<outlet property="timeLabel" destination="mHC-bT-4Ze" id="wYV-Wv-hKP"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
Expand Down
54 changes: 38 additions & 16 deletions Project 01 - SimpleStopWatch/StopWatch/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,55 +13,77 @@ class ViewController: UIViewController {

@IBOutlet weak var playBtn: UIButton!
@IBOutlet weak var pauseBtn: UIButton!
@IBOutlet weak var timeLabel: UILabel!

// 浮点数默认是Double类型,若要使用Float,需要显示声明
// var counter: Float = 0.0
var counter: Float = 0.0 {
// 属性观察器
didSet {
timeLabel.text = String(format: "%.1f", counter)
}
}
// 给予timer一个默认值,这样timer就不会为Optional,
// 后续可以不用再解包
// var timer = Timer()

var counter = 0.0
var timer = Timer()
// 这样定义可以在不用timer时回收内存
var timer: Timer? = Timer()
var isPlaying = false

func preferredStatusBarStyle() -> UIStatusBarStyle {
// 知识点:存储属性和计算属性
override var preferredStatusBarStyle: UIStatusBarStyle {
// 只读计算属性,可以去掉get和花括号
// get {
// return UIStatusBarStyle.lightContent
// }
return UIStatusBarStyle.lightContent
}

override func viewDidLoad() {

super.viewDidLoad()
// 符合LosslessStringConvertible协议的,
// 都可以直接初始化一个String对象
// timeLabel.text = String(counter)

timeLabel.text = String(counter)
// 改成使用属性观察器监控和响应属性值的变化
counter = 0.0
}

@IBOutlet weak var timeLabel: UILabel!
@IBAction func resetButtonDidTouch(_ sender: UIButton) {
timer.invalidate()
if let timerTemp = timer {
timerTemp.invalidate()
}
timer = nil
isPlaying = false
counter = 0
timeLabel.text = String(counter)
playBtn.isEnabled = true
pauseBtn.isEnabled = true
}

@IBAction func playButtonDidTouch(_ sender: UIButton) {
if(isPlaying) {
return
}
playBtn.isEnabled = false
pauseBtn.isEnabled = true
timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(ViewController.UpdateTimer), userInfo: nil, repeats: true)
// 调用实例的方法时建议用self.UpdateTimer,
// 不建议使用ViewController.UpdateTimer
// 因为若方法定义成了类方法,第二种方式编译器不会报错。
timer = Timer.scheduledTimer(timeInterval: 0.1, target:self, selector: #selector(self.UpdateTimer), userInfo: nil, repeats: true)
isPlaying = true
}

@IBAction func pauseButtonDidTouch(_ sender: UIButton) {

playBtn.isEnabled = true
pauseBtn.isEnabled = false
timer.invalidate()
if let timerTemp = timer {
timerTemp.invalidate()
}
timer = nil
isPlaying = false

}

@objc func UpdateTimer() {
counter = counter + 0.1
timeLabel.text = String(format: "%.1f", counter)

}


Expand Down

0 comments on commit 95259b3

Please sign in to comment.