User Interface 3D Transforms: Version 1.0, 2017-04-27

First version.

This sample code demonstrates applying 3D effects to an arbitrary UIView. A few different views are chosen randomly to facilitate this example: an image view, map view, and a web view. A view hierarchy is displayed that is akin to the tab switching user experience in Safari on iOS and the 3D nature of this effect is implemented using CALayer's CATransform3D.

Signed-off-by: Liu Lantao <liulantao@gmail.com>
This commit is contained in:
Liu Lantao 2017-10-16 22:37:21 +08:00
parent 9704241757
commit 9a81790657
No known key found for this signature in database
GPG Key ID: BF35AA0CD375679D
18 changed files with 997 additions and 0 deletions

View File

@ -0,0 +1,42 @@
Sample code project: User Interface 3D Transforms
Version: 1.0
IMPORTANT: This Apple software is supplied to you by Apple
Inc. ("Apple") in consideration of your agreement to the following
terms, and your use, installation, modification or redistribution of
this Apple software constitutes acceptance of these terms. If you do
not agree with these terms, please do not use, install, modify or
redistribute this Apple software.
In consideration of your agreement to abide by the following terms, and
subject to these terms, Apple grants you a personal, non-exclusive
license, under Apple's copyrights in this original Apple software (the
"Apple Software"), to use, reproduce, modify and redistribute the Apple
Software, with or without modifications, in source and/or binary forms;
provided that if you redistribute the Apple Software in its entirety and
without modifications, you must retain this notice and the following
text and disclaimers in all such redistributions of the Apple Software.
Neither the name, trademarks, service marks or logos of Apple Inc. may
be used to endorse or promote products derived from the Apple Software
without specific prior written permission from Apple. Except as
expressly stated in this notice, no other rights or licenses, express or
implied, are granted by Apple herein, including but not limited to any
patent rights that may be infringed by your derivative works or by other
works in which the Apple Software may be incorporated.
The Apple Software is provided by Apple on an "AS IS" basis. APPLE
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
Copyright (C) 2017 Apple Inc. All Rights Reserved.

View File

@ -0,0 +1,19 @@
# User Interface 3D Transforms
This sample code demonstrates applying 3D effects to an arbitrary UIView. A few different views are chosen randomly to facilitate this example: images, a map view, and a web view. A view hierarchy is shown that is akin to the tab switching user experience in Safari on iOS and the 3D nature of this effect is implemented using CALayer's CATransform3D.
Note regarding touch input:
Views that handle touch input continue to function normally while in the "switcher" mode by default in this sample code, and by that, they respond to touch input while transformed in 3D space. There are occasions where this might be desired bahavior, but in cases where it is not, set SwitcherView.enableUserInteractionInSwitcher to false.
## Requirements
### Build
Xcode 6 or later
### Runtime
iOS 8 or later
Copyright (C) 2015 Apple Inc. All rights reserved.

View File

@ -0,0 +1,321 @@
// !$*UTF8*$!
{
archiveVersion = 1;
classes = {
};
objectVersion = 46;
objects = {
/* Begin PBXBuildFile section */
30294F2E1E8594F1008AA164 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 30294F2D1E8594F1008AA164 /* AppDelegate.swift */; };
30294F301E8594F1008AA164 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 30294F2F1E8594F1008AA164 /* ViewController.swift */; };
30294F331E8594F1008AA164 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 30294F311E8594F1008AA164 /* Main.storyboard */; };
30294F351E8594F1008AA164 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 30294F341E8594F1008AA164 /* Assets.xcassets */; };
30294F381E8594F1008AA164 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 30294F361E8594F1008AA164 /* LaunchScreen.storyboard */; };
30294F461E8595F8008AA164 /* SwitcherView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 30294F451E8595F8008AA164 /* SwitcherView.swift */; };
30294F481E85C3FD008AA164 /* ContainerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 30294F471E85C3FD008AA164 /* ContainerView.swift */; };
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
30294F2A1E8594F1008AA164 /* UserInterface3DTransforms.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = UserInterface3DTransforms.app; sourceTree = BUILT_PRODUCTS_DIR; };
30294F2D1E8594F1008AA164 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
30294F2F1E8594F1008AA164 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
30294F321E8594F1008AA164 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
30294F341E8594F1008AA164 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
30294F371E8594F1008AA164 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
30294F391E8594F1008AA164 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
30294F451E8595F8008AA164 /* SwitcherView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SwitcherView.swift; sourceTree = "<group>"; };
30294F471E85C3FD008AA164 /* ContainerView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ContainerView.swift; sourceTree = "<group>"; };
30B7C1C51E89B07000C87C2A /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
30294F271E8594F1008AA164 /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXFrameworksBuildPhase section */
/* Begin PBXGroup section */
30294F211E8594F1008AA164 = {
isa = PBXGroup;
children = (
30B7C1C51E89B07000C87C2A /* README.md */,
30294F2C1E8594F1008AA164 /* UserInterface3DTransforms */,
30294F2B1E8594F1008AA164 /* Products */,
);
sourceTree = "<group>";
};
30294F2B1E8594F1008AA164 /* Products */ = {
isa = PBXGroup;
children = (
30294F2A1E8594F1008AA164 /* UserInterface3DTransforms.app */,
);
name = Products;
sourceTree = "<group>";
};
30294F2C1E8594F1008AA164 /* UserInterface3DTransforms */ = {
isa = PBXGroup;
children = (
30294F2D1E8594F1008AA164 /* AppDelegate.swift */,
30294F2F1E8594F1008AA164 /* ViewController.swift */,
30294F451E8595F8008AA164 /* SwitcherView.swift */,
30294F471E85C3FD008AA164 /* ContainerView.swift */,
30294F311E8594F1008AA164 /* Main.storyboard */,
30294F341E8594F1008AA164 /* Assets.xcassets */,
30294F361E8594F1008AA164 /* LaunchScreen.storyboard */,
30294F391E8594F1008AA164 /* Info.plist */,
);
path = UserInterface3DTransforms;
sourceTree = "<group>";
};
/* End PBXGroup section */
/* Begin PBXNativeTarget section */
30294F291E8594F1008AA164 /* UserInterface3DTransforms */ = {
isa = PBXNativeTarget;
buildConfigurationList = 30294F3C1E8594F1008AA164 /* Build configuration list for PBXNativeTarget "UserInterface3DTransforms" */;
buildPhases = (
30294F261E8594F1008AA164 /* Sources */,
30294F271E8594F1008AA164 /* Frameworks */,
30294F281E8594F1008AA164 /* Resources */,
);
buildRules = (
);
dependencies = (
);
name = UserInterface3DTransforms;
productName = UserInterface3DTransforms;
productReference = 30294F2A1E8594F1008AA164 /* UserInterface3DTransforms.app */;
productType = "com.apple.product-type.application";
};
/* End PBXNativeTarget section */
/* Begin PBXProject section */
30294F221E8594F1008AA164 /* Project object */ = {
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0820;
LastUpgradeCheck = 0820;
ORGANIZATIONNAME = Apple;
TargetAttributes = {
30294F291E8594F1008AA164 = {
CreatedOnToolsVersion = 8.2.1;
ProvisioningStyle = Automatic;
};
};
};
buildConfigurationList = 30294F251E8594F1008AA164 /* Build configuration list for PBXProject "UserInterface3DTransforms" */;
compatibilityVersion = "Xcode 3.2";
developmentRegion = English;
hasScannedForEncodings = 0;
knownRegions = (
en,
Base,
);
mainGroup = 30294F211E8594F1008AA164;
productRefGroup = 30294F2B1E8594F1008AA164 /* Products */;
projectDirPath = "";
projectRoot = "";
targets = (
30294F291E8594F1008AA164 /* UserInterface3DTransforms */,
);
};
/* End PBXProject section */
/* Begin PBXResourcesBuildPhase section */
30294F281E8594F1008AA164 /* Resources */ = {
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
30294F381E8594F1008AA164 /* LaunchScreen.storyboard in Resources */,
30294F351E8594F1008AA164 /* Assets.xcassets in Resources */,
30294F331E8594F1008AA164 /* Main.storyboard in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXResourcesBuildPhase section */
/* Begin PBXSourcesBuildPhase section */
30294F261E8594F1008AA164 /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
30294F481E85C3FD008AA164 /* ContainerView.swift in Sources */,
30294F301E8594F1008AA164 /* ViewController.swift in Sources */,
30294F461E8595F8008AA164 /* SwitcherView.swift in Sources */,
30294F2E1E8594F1008AA164 /* AppDelegate.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXSourcesBuildPhase section */
/* Begin PBXVariantGroup section */
30294F311E8594F1008AA164 /* Main.storyboard */ = {
isa = PBXVariantGroup;
children = (
30294F321E8594F1008AA164 /* Base */,
);
name = Main.storyboard;
sourceTree = "<group>";
};
30294F361E8594F1008AA164 /* LaunchScreen.storyboard */ = {
isa = PBXVariantGroup;
children = (
30294F371E8594F1008AA164 /* Base */,
);
name = LaunchScreen.storyboard;
sourceTree = "<group>";
};
/* End PBXVariantGroup section */
/* Begin XCBuildConfiguration section */
30294F3A1E8594F1008AA164 /* 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_MOVE = 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.2;
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;
};
30294F3B1E8594F1008AA164 /* 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_MOVE = 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.2;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
TARGETED_DEVICE_FAMILY = "1,2";
VALIDATE_PRODUCT = YES;
};
name = Release;
};
30294F3D1E8594F1008AA164 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
DEVELOPMENT_TEAM = "";
INFOPLIST_FILE = UserInterface3DTransforms/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.example.apple-samplecode.UserInterface3DTransforms";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
};
name = Debug;
};
30294F3E1E8594F1008AA164 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
DEVELOPMENT_TEAM = "";
INFOPLIST_FILE = UserInterface3DTransforms/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.example.apple-samplecode.UserInterface3DTransforms";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
};
name = Release;
};
/* End XCBuildConfiguration section */
/* Begin XCConfigurationList section */
30294F251E8594F1008AA164 /* Build configuration list for PBXProject "UserInterface3DTransforms" */ = {
isa = XCConfigurationList;
buildConfigurations = (
30294F3A1E8594F1008AA164 /* Debug */,
30294F3B1E8594F1008AA164 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
30294F3C1E8594F1008AA164 /* Build configuration list for PBXNativeTarget "UserInterface3DTransforms" */ = {
isa = XCConfigurationList;
buildConfigurations = (
30294F3D1E8594F1008AA164 /* Debug */,
30294F3E1E8594F1008AA164 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
rootObject = 30294F221E8594F1008AA164 /* Project object */;
}

View File

@ -0,0 +1,19 @@
/*
Copyright (C) 2017 Apple Inc. All Rights Reserved.
See LICENSE.txt for this samples licensing information
Abstract:
A default app delegate.
*/
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
private func application(_ application: UIApplication, didFinishLaunchingWithOptions: UIApplicationLaunchOptionsKey) -> Bool {
return true
}
}

View File

@ -0,0 +1,93 @@
{
"images" : [
{
"idiom" : "iphone",
"size" : "20x20",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "20x20",
"scale" : "3x"
},
{
"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" : "20x20",
"scale" : "1x"
},
{
"idiom" : "ipad",
"size" : "20x20",
"scale" : "2x"
},
{
"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"
},
{
"idiom" : "ipad",
"size" : "83.5x83.5",
"scale" : "2x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}

View File

@ -0,0 +1,6 @@
{
"info" : {
"version" : 1,
"author" : "xcode"
}
}

View File

@ -0,0 +1,21 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "image1.jpg",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 539 KiB

View File

@ -0,0 +1,21 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "image2.jpg",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 MiB

View File

@ -0,0 +1,21 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "image3.jpg",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 MiB

View File

@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="12118" systemVersion="16E195" 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="12086"/>
<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,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="12118" systemVersion="16E195" 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="12086"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--View Controller-->
<scene sceneID="tne-QT-ifu">
<objects>
<viewController id="BYZ-38-t0r" customClass="ViewController" customModule="UserInterface3DTransforms" 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"/>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</view>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
</objects>
</scene>
</scenes>
</document>

View File

@ -0,0 +1,96 @@
/*
Copyright (C) 2017 Apple Inc. All Rights Reserved.
See LICENSE.txt for this samples licensing information
Abstract:
The ContainerView is a UIView subclassed to add code that works in conjunction with the parentView (SwitcherView) to enable toggling between a full screen view mode, and "swicher" mode that allows switching to another full screen view.
*/
import UIKit
class ContainerView: UIView, UIGestureRecognizerDelegate {
var savedOriginY: CGFloat = 0.0
var parentView: SwitcherView
var isFullScreen: Bool = false
var index: Int = 0
var savedTransform: CATransform3D = CATransform3DIdentity
var tapRecognizer: UITapGestureRecognizer!
// Convenience initializer that sets up a web view.
convenience init(frame: CGRect, parentView: SwitcherView, urlString: String) {
self.init(frame: frame, parentView: parentView)
let webSubview = UIWebView(frame: bounds)
webSubview.isUserInteractionEnabled = SwitcherView.enableUserInteractionInSwitcher
addSubview(webSubview)
if let url = URL(string: urlString) {
webSubview.loadRequest(URLRequest(url: url))
} else {
fatalError("<!> could not create url with string: `\(urlString)`")
}
}
// Convenience initializer that sets takes a custom view as argument.
convenience init(frame: CGRect, parentView: SwitcherView, customView: UIView) {
self.init(frame: frame, parentView: parentView)
customView.isUserInteractionEnabled = SwitcherView.enableUserInteractionInSwitcher
addSubview(customView)
}
// Convenience initializer that sets an image as the layer's contents.
convenience init(frame: CGRect, parentView: SwitcherView, imageName: String) {
self.init(frame: frame, parentView: parentView)
self.layer.contents = UIImage(named: imageName)!.cgImage!
}
init(frame: CGRect, parentView: SwitcherView) {
self.parentView = parentView
super.init(frame: frame)
self.parentView = parentView
savedOriginY = frame.origin.y
// Create and initialize a tap gesture
tapRecognizer = UITapGestureRecognizer(target:self, action:#selector(toggleAnimation))
// Specify that the gesture must be a single tap
tapRecognizer.numberOfTapsRequired = 1
// Add the tap gesture recognizer to the view
addGestureRecognizer(tapRecognizer)
tapRecognizer.delegate = self
// Add a shadow to the cards
layer.shadowColor = UIColor.black.cgColor
layer.shadowOpacity = 0.5
layer.shadowPath = UIBezierPath(rect: bounds).cgPath
layer.shadowRadius = 20.0
// enable anti-aliasing
layer.edgeAntialiasingMask = CAEdgeAntialiasingMask(rawValue:
CAEdgeAntialiasingMask.layerLeftEdge.rawValue |
CAEdgeAntialiasingMask.layerRightEdge.rawValue |
CAEdgeAntialiasingMask.layerBottomEdge.rawValue |
CAEdgeAntialiasingMask.layerTopEdge.rawValue)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// Instruct that the tap recognizer is allowed to work in conjunction with vanilla UIView recognizers.
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith shouldRecognizeSimultaneouslyWithGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
// Called by the tap gesture recognizer. Ushers the parent view's toggle between the switcher and full screen modes.
func toggleAnimation() {
isFullScreen = !isFullScreen
parentView.animate(view: self, isFullScreen: isFullScreen)
parentView.scrollView.isScrollEnabled = !isFullScreen
// if user interaction should be disable in switcher mode, update that now
if !SwitcherView.enableUserInteractionInSwitcher {
for subview in subviews {
subview.isUserInteractionEnabled = isFullScreen
}
}
}
}

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>UIRequiresFullScreen</key>
<true/>
<key>UIStatusBarHidden</key>
<true/>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
</array>
</dict>
</plist>

View File

@ -0,0 +1,198 @@
/*
Copyright (C) 2017 Apple Inc. All Rights Reserved.
See LICENSE.txt for this samples licensing information
Abstract:
The switcher view demonstrates 3D transformations which is the primary focus of the sample.
*/
import UIKit
import MapKit
class SwitcherView: UIView, UIScrollViewDelegate {
// Determines whether views should react to touch input normally while in switcher mode, and 3D transformations are active.
static let enableUserInteractionInSwitcher = true
let animationDuration: TimeInterval = 0.2
let angle: CGFloat = 55.0 // amount to rotate the views in switcher mode
let scaleOut: CGFloat = 0.8 // amount to scale the views while in switcher mode
var containerViews: [ContainerView] = [] // array containing the various views
var scrollView: UIScrollView!
var switcherViewPadding: CGFloat = 0.0 // padding between views in switcher mode
let separatorDivisor: CGFloat = 3.0 // used during the switcher mode transition animation
var translationDivisor: CGFloat { // used during the switcher mode transition animation
get {
if UIDevice.current.userInterfaceIdiom == .pad {
return 4.46
} else {
return 4.13
}
}
}
override init(frame: CGRect) {
super.init(frame: frame)
standardInitialize()
}
required init(coder: NSCoder) {
super.init(coder: coder)!
standardInitialize()
}
func standardInitialize() {
// During switcher mode, the focus is on the 3D transforms; set a deemphasized background color accordingly.
layer.backgroundColor = UIColor.darkGray.cgColor
// Configure the scroll view used while switching views
switcherViewPadding = bounds.size.height / separatorDivisor
scrollView = UIScrollView(frame: bounds)
scrollView.isUserInteractionEnabled = true
scrollView.minimumZoomScale = 1.0
scrollView.maximumZoomScale = 1.0
scrollView.isScrollEnabled = true
scrollView.contentSize = CGSize(width: bounds.size.width, height: bounds.size.height)
scrollView.delegate = self
addSubview(scrollView)
// Fill the switcher view with a few different options: a couple image views, a map view, and a web view.
addContainerView(ContainerView(frame: bounds,
parentView: self,
imageName: "image1.jpg"))
addContainerView(ContainerView(frame: bounds,
parentView: self,
customView: MKMapView(frame: self.bounds)))
addContainerView(ContainerView(frame: bounds,
parentView: self,
imageName: "image2.jpg"))
addContainerView(ContainerView(frame: bounds,
parentView: self,
urlString: "http://www.apple.com"))
addContainerView(ContainerView(frame: bounds,
parentView: self,
imageName: "image3.jpg"))
}
// Helper function to convert degrees (user friendly input) to radians (the argument for CATransorm3D rotations)
func radians(degrees: CGFloat) -> CGFloat {
return degrees * CGFloat.pi / 180.0
}
/**
Container views are regular views that show various example content while facilitating the purpose of demonstrating
3D tranformations - the primary objective of this sample.
*/
func addContainerView(_ view: ContainerView) {
view.layer.shadowPath = UIBezierPath(rect: view.bounds).cgPath
view.layer.shadowColor = UIColor.black.cgColor
view.frame = CGRect(x: 0.0,
y: CGFloat(scrollView.subviews.count) * switcherViewPadding,
width: frame.size.width,
height: frame.size.height)
scrollView.addSubview(view)
view.index = containerViews.count
containerViews.append(view)
view.layer.transform = getTransform(translatedToY: 0,
isScale: true,
isRotate: true)
scrollView.contentSize = CGSize(width: scrollView.contentSize.width,
height: scrollView.contentSize.height + switcherViewPadding)
}
/**
Create and configure a new CATransform3D.
Parameters:
tranlatedToY - most views are translated during the switcher view transition animation.
The cards above the tapped card are moved upward, and the cards below are moved downward.
scale - the views undergo a scale animation being sized down in the switcher view to increase visibility
rotate - the views are tappered in switcher mode to give 3D perspected and rotation is how that's implemented
*/
func getTransform(translatedToY: CGFloat, isScale: Bool, isRotate: Bool) -> CATransform3D {
var transform = CATransform3DIdentity
// m34 enables 3D on the transform. The divisor determines the distance in the z-direction.
transform.m34 = 1.0 / -2000.0
if isRotate {
transform = CATransform3DRotate(transform, -radians(degrees: angle), 1.0, 0.0, 0.0)
}
if isScale {
transform = CATransform3DScale(transform, scaleOut, scaleOut, scaleOut)
}
if translatedToY != 0.0 {
transform = CATransform3DTranslate(transform, 0.0, translatedToY, 0.0)
}
return transform
}
/**
Performs the transform animation.
When a CAAnimation is active, the model layer is hidden and its presentationLayer is shown which visualizes the animation.
Upon animation completion, the presentationLayer is hidden and the model layer is present in the animation's final state.
This is standard Core Animation behavior.
*/
func animate(transform: CATransform3D, view: UIView) {
let basicAnim = CABasicAnimation(keyPath: "transform")
// From value is the view's current 3D transform
basicAnim.fromValue = view.layer.transform
// To value is the argument new transform
basicAnim.toValue = transform
basicAnim.duration = animationDuration
view.layer.add(basicAnim, forKey: nil)
// Set the model layer to the final value. This change won't be visible until the animation done above completes
// disable actions so the following transform change does not trigger an implicit animation
CATransaction.setDisableActions(true)
view.layer.transform = transform
CATransaction.setDisableActions(false)
}
func animate(view: ContainerView, isFullScreen: Bool) {
// the transforms are different depending on whether the tapped view is currently full screen or in switcher mode
if isFullScreen {
// cards above. Tapped view is at view.index, so end iteration at view.index-1
for i in 0..<view.index {
animate(transform: getTransform(translatedToY: -bounds.size.height,
isScale: true,
isRotate: false),
view: containerViews[i])
}
// tapped card
animate(transform: getTransform(
translatedToY: scrollView.contentOffset.y - (view.frame.origin.y - view.bounds.size.height / translationDivisor),
isScale: false,
isRotate: false),
view: view)
// cards below. Tapped view is at view.index, so start iteration at view.index+1
for i in (view.index + 1)..<containerViews.count {
animate(transform: getTransform(translatedToY: 2.0 * bounds.size.height,
isScale: true,
isRotate: true),
view: containerViews[i])
}
} else {
// cards above. Tapped view is at view.index, so end iteration at view.index-1
for i in 0..<view.index {
animate(transform: getTransform(translatedToY: 0.0,
isScale: true,
isRotate: true),
view: containerViews[i])
}
// tapped card
animate(transform: getTransform(translatedToY: 0.0,
isScale: true,
isRotate: true),
view: view)
// cards below. Tapped view is at view.index, so start iteration at view.index+1
for i in (view.index + 1)..<containerViews.count {
animate(transform: getTransform(translatedToY: 0.0,
isScale: true,
isRotate: true),
view: containerViews[i])
}
}
}
}

View File

@ -0,0 +1,34 @@
/*
Copyright (C) 2017 Apple Inc. All Rights Reserved.
See LICENSE.txt for this samples licensing information
Abstract:
The app's view controller.
*/
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
view.addSubview( SwitcherView( frame: UIScreen.main.bounds ) )
}
// Rotation is disabled for the purposes of this sample.
override var shouldAutorotate: Bool {
get { return false }
}
// Status bar is hidden for maximum visibility of the demonstration.
override var prefersStatusBarHidden: Bool {
get { return true }
}
// For the purposes of this sample, the controller locks orientation to portrait mode.
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
get { return .portrait }
}
}