Swift and swiftcode

Source: Internet
Author: User

Swift and swiftcode

Swift-use CATransform3DMakeRotation to achieve page turning

 

Effect

 

Source code

Https://github.com/YouXianMing/Swift-Animations

/// Pageflippolictcontroller. swift // Swift-Animations // Created by YouXianMing on 16/8/22. // Copyright©2016 YouXianMing. All rights reserved. // import UIKitclass PageFlipEffectController: NormalTitleViewController {private var math: Math! = Math (pointA: CGPointMake (0, 0), pointB: CGPointMake (Width (), 180) private var layer: CALayer! Override func setup () {super. setup () let image = UIImage (named: "pic_1") let size = Math. ResetFromSize (image ?. Size )!, WithFixedWidth: Width ()/2.0) layer = CALayer () layer. anchorPoint = CGPointMake (1.0, 0.5) layer. frame = CGRectMake (0, 0, Width ()/2, size. height) layer. allowsEdgeAntialiasing = true layer. position = CGPointMake (Width ()/2, contentView !. MiddleY) layer. contents = image ?. CGImage layer. borderColor = UIColor. blackColor (). CGColor layer. borderWidth = 3.0 layer. masksToBounds = true layer. transform = CATransform3DMakeRotation (Math. radianFromDegree (0), 0, 1, 1) contentView ?. Layer. addSublayer (layer) let panGesture = UIPanGestureRecognizer (target: self, action: # selector (pageflippolictcontroller. handlePan) view. addGestureRecognizer (panGesture)} @ objc private func handlePan (sender: UIPanGestureRecognizer) {let curPoint = sender. locationInView (view) let x = curPoint. x // initialize the 3D transformation and obtain the default value var perspectiveTransform = CATransform3DIdentity // view perspectiveTransform. m34 =-1. 0/2000.0 // space rotation perspectiveTransform = CATransform3DRotate (perspectiveTransform, Math. radianFromDegree (x * math. k), 0, 1, 0) CATransaction. setDisableActions (true) layer. transform = perspectiveTransform layer. contents = UIImage (named: x & gt; = Width ()/2.0? "Pic_2": "pic_1 ")?. CGImage if sender. state =. ended {// initialize the 3D transform and obtain the default value var perspectiveTransform = CATransform3DIdentity // view perspectiveTransform. m34 =-1.0/2000.0 // space rotation perspectiveTransform = CATransform3DRotate (perspectiveTransform, Math. radianFromDegree (x & gt; = Width ()/2.0? 180: 0), 0, 1, 0) CATransaction. setDisableActions (false) layer. transform = perspectiveTransform }}}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.