• Stars
    star
    580
  • Rank 77,010 (Top 2 %)
  • Language
    Swift
  • License
    MIT License
  • Created over 7 years ago
  • Updated almost 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

AlertTransition is a extensible library for making view controller transitions, especially for alert transitions.

AlertTransition

Xcode 8.2+ iOS 8.0+ Swift 3.0+ Swift 4 Carthage compatible Version

AlertTransition is a extensible library for making view controller transitions, especially for alert transitions.

Overview

AlertTransition is a super class, make basic structure, and no default animation realized.

EasyTransition is a subclass of AlertTransition. with it, you can compose custom transition with translation, scale, rotation and alpha. You can also change background with custom color or UIBlurEffect. It support changing device orientation. And it is easy to use both in code or storyboard.

MenuTransition is a subclass of AlertTransition. With it, you can make a side menu with a few line of codes.

TrolleyTransition is a subclass of AlertTransition. With it, you can make a trolley with a few line of codes.

You can subclass AlertTransition, and write your custom transition. And you can also wrap other transition effect with AlertTransition, such as BubbleTransition or StarWars.iOS.

AlertTransition

How To Use

It is pretty simple to use AlertTransition

// First, initialize your presented controller
let alert = SomeController()
// Second, initialize a subclass of AlertTransition, such as EasyTransition, configure your controller with it
alert.at.transition = EasyTransition()
// Present your controller, Amazing!!
present(alert, animated: true, completion: nil)

If you use storyboard

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

	// First, find presented controller
	let controller = segue.destination 
	// Second, initialize a subclass of AlertTransition, such as EasyTransition, configure your controller with it  
	controller.at.transition = EasyTransition()
}
@IBAction func dismissButtonClicked() {
    dismiss(animated: true, completion: nil)
}

Frame of Presented Controller

How to set the frame of presented controller? Conform to AlertFrameProtocol or Self-sizing

AlertFrameProtocol

Conforms to AlertFrameProtocol, provide your desired frame through property alertFrame

class SomeController: UIViewController, AlertFrameProtocol {
    
    var alertFrame: CGRect {
        let x = (UIScreen.main.bounds.size.width - 200) / 2
        let y = (UIScreen.main.bounds.size.height - 250) / 2
        return CGRect(x: x, y: y, width: 200, height: 250)
    }
}

Self-sizing

You need an unbroken chain of constraints and views (with defined heights) to fill the area between the controller viewโ€™s top edge and its bottom edge, and you also need one (with defined widths) between the controller viewโ€™s left edge and its right edge. If you know how to write Self-sizing Cell, this will be easy to you.

AlertTransition will calculate view's width and height, and set the presented controller in center of screen.

Sometimes, there maybe constraint conflicts. Among the chain of constraints, pick one constraint and reduce it's priority (eg: from 1000 to 999), everything will be fine.

You can find demo code in SnapKitAlertController.swift and Main.storyboard.

Change background

You can change alert background with backgroundType. Effect gif is the second image above.

let alert = SomeController()

// It is a property of AlertTransition, you can use any subclass, just use EasyTransition as an example
let transition = EasyTransition()
transition.backgroundType = .blurEffect(style: .extraLight, alpha: 0.9)
// transition.backgroundType = .blurEffect(style: .light, alpha: 0.9)
// transition.backgroundType = .blurEffect(style: .dark, alpha: 0.9)
// transition.backgroundType = .color(UIColor.blue.withAlphaComponent(0.5))
    
alert.at.transition = transition
present(alert, animated: true, completion: nil)

EasyTransition

You can easily compose your custom transition with a enum named AnimationType

let alert = SomeController()
let transition = EasyTransition()
transition.startTransforms = [.rotation(angle: CGFloat(Double.pi/2), anchorPoint: CGPoint(x: 0, y: 0)), .alpha(0)]
alert.at.transition = transition
present(alert, animated: true, completion: nil)

EasyTransition use startTransforms and final frame of presented controller's view, to calculate the state of start. The above sample code, the view will rotate pi / 2 with anchoPoint in left top corner, and alpha 0.

There is also a property named endTransforms, it will have same value with startTransforms in default.

You can also change duration, damping, velocity, curve of the animation with presentAnimateParams and dismissAnimateParams

let alert = SomeController()
let transition = EasyTransition()
transition.presentAnimateParams.damping = 0.3
alert.at.transition = transition
present(alert, animated: true, completion: nil)

MenuTransition

There are only three steps to make a side menu. Set frame and change background just like other AlertTransition.

class MainController: UIViewController {
    // First, hold your menu controller in your main controller
    var menuController = MenuController()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // second, initialize MenuTransition with presenting controller
        let transition = MenuTransition(from: navigationController)
        // third, set MenuTransition to your menuController
        menuController.at.transition = transition
    }
}

When you select an item at side menu, you want to push a controller from main controller. Push like this to make suitable animation.

(self.at.transition as? MenuTransition)?.push(controller: NextViewController())

Write custom AlertTransition

In most cases, you only need to override func performPresentedTransition and performDismissedTransition. Apply your animation to presentingView and presentedView, such as TrolleyTransition:

public class TrolleyTransition: AlertTransition {

    public override init(from controller: UIViewController?) {
        super.init(from: controller)
        duration = 0.5
    }
    
    public override func performPresentedTransition(presentingView: UIView, presentedView: UIView, context: UIViewControllerContextTransitioning) {
        presentedView.frame.origin.y = UIScreen.main.bounds.height
        
        UIView.animate(withDuration: duration/2, animations: {
            presentingView.layer.transform = self.firstTransform()
        }) { (complete) in
            UIView.animate(withDuration: self.duration/2, animations: {
                presentingView.layer.transform = self.secondTransform()
                presentedView.transform = CGAffineTransform(translationX: 0, y: -presentedView.frame.height)
            }, completion: { (complete) in
                context.completeTransition(complete)
            })
        }
    }
    
    public override func performDismissedTransition(presentingView: UIView, presentedView: UIView, context: UIViewControllerContextTransitioning) {
        
        UIView.animate(withDuration: duration/2, animations: {
            presentedView.transform = CGAffineTransform.identity
            presentingView.layer.transform = self.firstTransform()
        }) { (complete) in
            UIView.animate(withDuration: self.duration/2, animations: {
                presentingView.layer.transform = CATransform3DIdentity
            }, completion: { (complete) in
                context.completeTransition(complete)
            })
        }
    }
    
    private func firstTransform() -> CATransform3D {
        var form = CATransform3DIdentity
        form.m34 = 1.0 / -900
        form = CATransform3DScale(form, 0.9, 0.9, 1)
        form = CATransform3DRotate(form, 15.0 * CGFloat(Double.pi)/180.0, 1, 0, 0)
        form = CATransform3DTranslate(form, 0, 0, -100.0)
        return form
    }
    
    private func secondTransform() -> CATransform3D {
        var form = CATransform3DIdentity
        form.m34 = firstTransform().m34
        form = CATransform3DTranslate(form, 0, -20, 0)
        form = CATransform3DScale(form, 0.9, 0.9, 1)
        return form
    }
}

If you want add UIPercentDrivenInteractiveTransition, or has custom UIPresentationController, set interactionTransitionType and presentationControllerType at init method.

public override init(from controller: UIViewController? = nil) {
    super.init(from: controller)
    
    interactionTransitionType = EasyPercentDrivenTransition.self
    presentationControllerType = SomePresentationController.self
}

If you write an amazing custom transition, please submit a pull requests. We looking forward to accumulate custom transitions. And with AlertTransition, we can easily change from one custom transition to another.

Getting involved

  • If you want to contribute please feel free to submit pull requests, even if you find some spell error in README, because I am not good at English.
  • If you have a feature request please open an issue.
  • If you found a bug or need help please check older issues before submitting an issue.

Installation

CocoaPods

CocoaPods is a dependency manager for Cocoa projects.

Specify AlertTransition into your project's Podfile:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!

target '<Your App Target>' do
  #Swift3
  #pod 'AlertTransition', "~> 1.0.4"
  #Swift4
  pod 'AlertTransition', "~> 2.1.0"
end

If you only want EasyTransition, MenuTransition or TrolleyTransition, you can pod them alone like this:

pod 'AlertTransition/Easy', "~> 2.1.0"
pod 'AlertTransition/Menu', "~> 2.1.0"
pod 'AlertTransition/Trolley', "~> 2.1.0"

Then run the following command:

$ pod install

Carthage

Carthage is a simple, decentralized dependency manager for Cocoa.

Specify AlertTransition into your project's Carthage:

github "loopeer/AlertTransition" ~> 2.1.0

More Repositories

1

CardStackView

One Custom view for show something just like cards with animations.
Java
2,179
star
2

shadow

Deprecated because of the performance not fine. Android custom shadow view, can replace your CardView
Kotlin
1,371
star
3

itemtouchhelper-extension

Extension for itemtouchhelper with swipe settling,recover and no conflict with recyclerview
Java
1,040
star
4

code-reader

One Multi program language code reader
Kotlin
947
star
5

SpringHeader

An implementation of spring-like header (known as pull-to-refresh) that using CoordinatorLayout.
Java
177
star
6

ScrollTableView

Android one demo of horizontal and vertical scroll custom table view
Java
112
star
7

ImageGroupView

Android image group view. One android custom view you can show and add image
Java
73
star
8

PullSwitcher

Help to switch fragment easier
Java
50
star
9

LoginLib

ๅฏน็™ปๅฝ•ๆณจๅ†Œไธญๅ‘้€้ชŒ่ฏ็ ๆŒ‰้’ฎๅ’ŒๆไบคๆŒ‰้’ฎ็š„ๅฐ่ฃ…ใ€‚
Java
29
star
10

MultiTextTagView

Android Multi Tag View
Java
23
star
11

quickcms

laravel quick cms
HTML
20
star
12

LPAlbum

an albums that can preview, multiple-choice
Swift
17
star
13

AutoLoopPager

One library can make pager slide loop, and add your custom view easier.
Java
17
star
14

AttributedStringWrapper

a simple packaging for NSAttributedString to make the developers easy to use
Swift
16
star
15

CoordinateLayoutDemo

Android One Test of CoordinatorLayout
Java
14
star
16

ProjectDevelopModule

Java
11
star
17

AndroidLearnNote

Some note from learning Android
Java
8
star
18

PullToRefreshAndLoadMore

Android pull to refresh, load more
Java
3
star
19

android-kotlin-ext

One library of kotlin extension functions for android
Kotlin
3
star
20

easypush

A package for push messages
PHP
2
star
21

android-plugin-loopeermodel

an Android Studio plugin for generating model using loopeer data dictionary.
Java
1
star
22

QRCodeScan

Java
1
star
23

FilterDropMenu

Java
1
star
24

bangduobao

ๅธฎๅคšๅฎ้ฆ–้กต้™ๆ€ๆ–‡ไปถ
HTML
1
star
25

LoopeerLogin

Java
1
star
26

DriveIndicator

Java
1
star