Skip to content

Commit

Permalink
Day 15- 首页加载动画
Browse files Browse the repository at this point in the history
1.支持swift4
2.动画bug修复
  • Loading branch information
qianyb committed Dec 25, 2018
1 parent f15389f commit b925cb6
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@
TargetAttributes = {
9F5E2F221C4FCE68003B68A0 = {
CreatedOnToolsVersion = 7.2;
DevelopmentTeam = F3YT8BWBYJ;
};
9F5E2F361C4FCE68003B68A0 = {
CreatedOnToolsVersion = 7.2;
Expand Down Expand Up @@ -378,21 +379,27 @@
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
DEVELOPMENT_TEAM = F3YT8BWBYJ;
INFOPLIST_FILE = AnimatedSplash/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = me.appkitchen.AnimatedSplash;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 4.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Debug;
};
9F5E2F4D1C4FCE68003B68A0 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
DEVELOPMENT_TEAM = F3YT8BWBYJ;
INFOPLIST_FILE = AnimatedSplash/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = me.appkitchen.AnimatedSplash;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 4.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Release;
};
Expand Down Expand Up @@ -463,6 +470,7 @@
9F5E2F4D1C4FCE68003B68A0 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
9F5E2F4E1C4FCE68003B68A0 /* Build configuration list for PBXNativeTarget "AnimatedSplashTests" */ = {
isa = XCConfigurationList;
Expand All @@ -471,6 +479,7 @@
9F5E2F501C4FCE68003B68A0 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
9F5E2F511C4FCE68003B68A0 /* Build configuration list for PBXNativeTarget "AnimatedSplashUITests" */ = {
isa = XCConfigurationList;
Expand All @@ -479,6 +488,7 @@
9F5E2F531C4FCE68003B68A0 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
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>
62 changes: 10 additions & 52 deletions Project 15 - AnimatedSplash/AnimatedSplash/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,78 +12,36 @@ import UIKit
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?
var mask: CALayer?
var imageView: UIImageView?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]? = nil) -> Bool {

self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window?.rootViewController = UIViewController()

let imageView = UIImageView(frame: self.window!.frame)
imageView.image = UIImage(named: "screen")
self.window!.addSubview(imageView)


self.mask = CALayer()
self.mask!.contents = UIImage(named: "twitter")?.CGImage
self.mask!.contentsGravity = kCAGravityResizeAspect
self.mask!.bounds = CGRect(x: 0, y: 0, width: 100, height: 81)
self.mask!.anchorPoint = CGPoint(x: 0.5, y: 0.5)
self.mask!.position = CGPoint(x: imageView.frame.size.width / 2, y: imageView.frame.size.height / 2)
imageView.layer.mask = mask
self.imageView = imageView

animateMask()

self.window!.backgroundColor = UIColor(red:0.117, green:0.631, blue:0.949, alpha:1)
self.window!.makeKeyAndVisible()
UIApplication.sharedApplication().statusBarHidden = true
// self.window = UIWindow(frame: UIScreen.main.bounds)
// self.window!.rootViewController = ViewController()
// self.window!.backgroundColor = UIColor(red:0.117, green:0.631, blue:0.949, alpha:1)
// self.window!.makeKeyAndVisible()
return true
}

func applicationWillResignActive(application: UIApplication) {
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 throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

func applicationDidEnterBackground(application: UIApplication) {
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) {
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

func applicationDidBecomeActive(application: UIApplication) {
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) {
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

func animateMask() {

let keyFrameAnimation = CAKeyframeAnimation(keyPath: "bounds")
keyFrameAnimation.delegate = self
keyFrameAnimation.duration = 0.6
keyFrameAnimation.beginTime = CACurrentMediaTime() + 0.5
keyFrameAnimation.timingFunctions = [CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut), CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)]
let initalBounds = NSValue(CGRect: mask!.bounds)
let secondBounds = NSValue(CGRect: CGRect(x: 0, y: 0, width: 90, height: 73))
let finalBounds = NSValue(CGRect: CGRect(x: 0, y: 0, width: 1600, height: 1300))
keyFrameAnimation.values = [initalBounds, secondBounds, finalBounds]
keyFrameAnimation.keyTimes = [0, 0.3, 1]
self.mask!.addAnimation(keyFrameAnimation, forKey: "bounds")

}

override func animationDidStop(anim: CAAnimation, finished flag: Bool) {
self.imageView!.layer.mask = nil
}


}

Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
{
"images" : [
{
"idiom" : "iphone",
"size" : "20x20",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "20x20",
"scale" : "3x"
},
{
"idiom" : "iphone",
"size" : "29x29",
Expand Down Expand Up @@ -29,6 +39,11 @@
"idiom" : "iphone",
"size" : "60x60",
"scale" : "3x"
},
{
"idiom" : "ios-marketing",
"size" : "1024x1024",
"scale" : "1x"
}
],
"info" : {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
},
{
"idiom" : "universal",
"filename" : "IMG_2112.PNG",
"filename" : "IMG_2112.png",
"scale" : "2x"
},
{
Expand Down
12 changes: 6 additions & 6 deletions Project 15 - AnimatedSplash/AnimatedSplash/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>LSApplicationCategoryType</key>
<string></string>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
Expand All @@ -22,6 +20,8 @@
<string>????</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSApplicationCategoryType</key>
<string></string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UILaunchStoryboardName</key>
Expand All @@ -32,13 +32,13 @@
<array>
<string>armv7</string>
</array>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UIViewControllerBasedStatusBarAppearance</key>
<true/>
<key>UIStatusBarHidden</key>
<true/>
</dict>
</plist>
19 changes: 11 additions & 8 deletions Project 15 - AnimatedSplash/AnimatedSplash/LaunchScreen.storyboard
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="9531" systemVersion="15E33e" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" initialViewController="01J-lp-oVM">
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14460.31" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="01J-lp-oVM">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="9529"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14460.20"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--View Controller-->
Expand All @@ -18,27 +22,26 @@
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="twitter" translatesAutoresizingMaskIntoConstraints="NO" id="CEh-BR-p0S">
<rect key="frame" x="137" y="293" width="100" height="81"/>
<rect key="frame" x="137.5" y="293" width="100" height="81"/>
<constraints>
<constraint firstAttribute="width" constant="100" id="0io-Xw-zxo"/>
<constraint firstAttribute="height" constant="81" id="UD6-4D-7Fn"/>
</constraints>
</imageView>
</subviews>
<color key="backgroundColor" red="0.0" green="0.64315026998519897" blue="0.95932561159133911" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<color key="backgroundColor" red="0.070588235294117646" green="0.56862745098039214" blue="0.94901960784313721" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstItem="CEh-BR-p0S" firstAttribute="centerX" secondItem="Ze5-6b-2t3" secondAttribute="centerX" id="C5W-2z-wC6"/>
<constraint firstItem="CEh-BR-p0S" firstAttribute="centerY" secondItem="Ze5-6b-2t3" secondAttribute="centerY" id="JTz-FK-oB7"/>
<constraint firstItem="CEh-BR-p0S" firstAttribute="top" secondItem="Llm-lL-Icb" secondAttribute="bottom" constant="273" id="kJl-rG-e5i"/>
</constraints>
</view>
<simulatedScreenMetrics key="simulatedDestinationMetrics" type="retina47"/>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="iYj-Kq-Ea1" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="53" y="375"/>
</scene>
</scenes>
<resources>
<image name="twitter" width="569" height="463"/>
<image name="twitter" width="273.3599853515625" height="222.24000549316406"/>
</resources>
</document>
34 changes: 28 additions & 6 deletions Project 15 - AnimatedSplash/AnimatedSplash/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,27 +1,49 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="9531" systemVersion="15E33e" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="0pO-6z-HlI">
<?xml version="1.0" encoding="UTF-8"?>
<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="0pO-6z-HlI">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="9529"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14460.20"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--View Controller-->
<scene sceneID="xLH-lH-Xz4">
<objects>
<viewController id="0pO-6z-HlI" sceneMemberID="viewController">
<viewController extendedLayoutIncludesOpaqueBars="YES" id="0pO-6z-HlI" customClass="ViewController" customModule="AnimatedSplash" customModuleProvider="target" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="O1c-Hc-pRg"/>
<viewControllerLayoutGuide type="bottom" id="Yf6-fZ-f6k"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="HJO-SG-hlk">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" red="0.99990576505661011" green="1" blue="0.99987310171127319" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<subviews>
<imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="screen" translatesAutoresizingMaskIntoConstraints="NO" id="NaD-wG-BNY">
<rect key="frame" x="0.0" y="20" width="375" height="647"/>
</imageView>
</subviews>
<color key="backgroundColor" red="0.070588235294117646" green="0.56862745098039214" blue="0.94901960784313721" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstAttribute="trailing" secondItem="NaD-wG-BNY" secondAttribute="trailing" id="gaK-sb-T68"/>
<constraint firstItem="NaD-wG-BNY" firstAttribute="top" secondItem="O1c-Hc-pRg" secondAttribute="bottom" id="iRt-o6-i3a"/>
<constraint firstItem="NaD-wG-BNY" firstAttribute="leading" secondItem="HJO-SG-hlk" secondAttribute="leading" id="ivA-8o-CQm"/>
<constraint firstItem="Yf6-fZ-f6k" firstAttribute="top" secondItem="NaD-wG-BNY" secondAttribute="bottom" id="wjD-ha-xXN"/>
</constraints>
</view>
<nil key="simulatedTopBarMetrics"/>
<connections>
<outlet property="imageView" destination="NaD-wG-BNY" id="0vI-HI-oEU"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="b7y-ow-oYb" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="381" y="399"/>
</scene>
</scenes>
<resources>
<image name="screen" width="750" height="1334"/>
</resources>
</document>
46 changes: 44 additions & 2 deletions Project 15 - AnimatedSplash/AnimatedSplash/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,59 @@
import UIKit

class ViewController: UIViewController {

@IBOutlet weak var imageView: UIImageView!

var mask: CALayer?

override var prefersStatusBarHidden: Bool {
return true
}

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

self.mask = CALayer()
self.mask!.contents = UIImage(named: "twitter")?.cgImage
self.mask!.contentsGravity = kCAGravityResizeAspect
self.mask!.bounds = CGRect(x: 0, y: 0, width: 100, height: 81)
self.mask!.anchorPoint = CGPoint(x: 0.5, y: 0.5)
self.imageView.setNeedsLayout()
self.imageView.layoutIfNeeded()
self.mask!.position = CGPoint(x: self.imageView.frame.size.width / 2, y: self.imageView.frame.size.height / 2)
self.imageView.layer.mask = mask

animateMask()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}



func animateMask() {
let keyFrameAnimation = CAKeyframeAnimation(keyPath: "bounds")
keyFrameAnimation.delegate = self
keyFrameAnimation.duration = 0.6
keyFrameAnimation.beginTime = CACurrentMediaTime() + 0.5
keyFrameAnimation.timingFunctions = [CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut), CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)]
do {
// 动画需要加上这段代码,否则会造成页面闪一下
keyFrameAnimation.fillMode = kCAFillModeForwards
keyFrameAnimation.isRemovedOnCompletion = false
}
let initalBounds = NSValue(cgRect: mask!.bounds)
let secondBounds = NSValue(cgRect: CGRect(x: 0, y: 0, width: 90*0.9, height: 73*0.9))
let finalBounds = NSValue(cgRect: CGRect(x: 0, y: 0, width: 1600, height: 1300))
keyFrameAnimation.values = [initalBounds, secondBounds, finalBounds]
keyFrameAnimation.keyTimes = [0, 0.3, 1]
self.mask!.add(keyFrameAnimation, forKey: "bounds")
}
}

extension ViewController : CAAnimationDelegate {
func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
self.imageView.layer.mask = nil
}
}

0 comments on commit b925cb6

Please sign in to comment.