Swift Custom transition animations (more difficult)

Source: Internet
Author: User

A transition and Transition mode 1 transitions:

2 Adoption Method (method):--> 2.1 Custom Transition Animation--2.2 Protocol two transition implementations need to get something 1 get the frame2 of the pre-transition picture set a temporary imageview as the transition picture (the picture is not real) 3 Get the picture to enlarge the frame three transitions diagram

Four transition animation ideas 1 by defining the protocol method in the class that implements the transition animation, define the proxy properties, identify who can provide the required frame and ImageView, set the other as the proxy, let the agent provide the demand, achieve the transition purpose. 2 Specify where the code is written, and where you can implement the transition animation. Five protocol (Popup animation) 1 protocol writing position

2 Protocol Method: (Can let the outside world to provide the necessary things)
//MARKclass {    //起始位置    func homeRect(indexPath : NSIndexPath) ->CGRect    //展示图片的位置    func photoBrowserRect(indexPath : NSIndexPath) ->CGRect    //获取imageView(用这张临时的图片来实现动画效果)    func imageView(indexPath : NSIndexPath) ->UIImageView}
3 Set the proxy in the class
//设置代理(代理一般都是用weak修饰)    weakvar presentDelegate : PersentDelegate?
4 the code where the Transition animation is implemented: (the previous post has explained where the transition animation is written)--4.1 Get a picture--4.2 Get the start of the picture in home frame--> 4.3 show picture of frame--> 4.4 complete the animation--& Gt 4.5 images from transparent to clear, with Alpha to achieve
//Gets the context of the transition: you can get to the view that performs the animation through the contextFunc animatetransition (transitioncontext:uiviewcontrollercontexttransitioning) {ispresented?Animateforpresentview (Transitioncontext): animatefordismissed (Transitioncontext)}//EjectFunc Animateforpresentview (transitioncontext:uiviewcontrollercontexttransitioning) {//1 Remove pop-up view         LetPresentview=Transitioncontext.Viewforkey (Uitransitioncontexttoviewkey)!        //2 Add pop-up view to ContentviewTransitioncontext.Containerview ()?.Addsubview (Presentview)//3 performing animations        //3.1 Get the imageview you need to performGuard LetPersentdelegate=PresentdelegateElse{return} Guard LetIndexpath=IndexpathElse{return}//Call method to get a picture         LetImageView=Persentdelegate.ImageView (Indexpath)//Add a picture to the parent controlTransitioncontext.Containerview ()?.Addsubview (ImageView)//Set the starting position of the ImageViewImageView.Frame=Persentdelegate.Homerect (Indexpath) Presentview.Alpha= 0        //Set the background color to be black when the popup animationTransitioncontext.Containerview ()?.BackgroundColor=Uicolor.Blackcolor () UIView.Animatewithduration (Transitionduration (Transitioncontext), animations: {() -Void inch            //Set the location of the display pictureImageView.Frame=Persentdelegate.Photobrowserrect (Indexpath)}) {(_) -Void inch            //Modify Picture TransparencyPresentview.Alpha= 1.0            //Remove picturesImageView.Removefromsuperview () Transitioncontext.Containerview ()?.BackgroundColor=Uicolor.Clearcolor ()//Finish animationTransitioncontext.Completetransition (true)        }    }
Six-pass value 1 How to get the frame value of a picture in home by agreement?—-> 1.1 by setting a indexpath (for external incoming corners) in the method that animates the UN wipe, you can determine the frame
class PhotoBrowserAnimator: NSObject {    //设置角标    var indexPath : NSIndexPath?}
2 Setting up Proxy objects

//Mark: - 点击图片,弹出控制器extension HomeViewController {        //定义为私有的        privateshowPhotoBrowser( indexPath : NSIndexPath) {        //将图片的角标传入        photoBrowserAnimator.indexPath = indexPath        //设置代理        photoBrowserAnimator.presentDelegate = self}
3 Implement the Protocol method (this part of the code needs to write more, so expand a method)-3.1 Get the picture in the home frame value (focus on: How to convert the cell's frame value to the cell's coordinate value in window)
//MARK: - 实现协议中的方法extension HomeViewController : PersentDelegate {    NSIndexPathCGRect {        //取出cell         let cell = (collectionView?.cellForItemAtIndexPath(indexPath))!        //将cell的frame值转成cell在window中的坐标        let homeFrame = collectionView!.convertRect(cell.frameUIApplication.sharedApplication().keyWindow!)        //返回具体的尺寸        return homeFrame    }}
--3.2 Returns a temporary picture as a transition animation (written in the same method)
func ImageView (Indexpath:nsindexpath), Uiimageview {//Create im Ageview object  let  ImageView = Uiimageview ()  Remove the cell  let  cell = (CollectionView?. Cellforitematindexpath (Indexpath))! as ! Homeviewcell //remove picture displayed in cell  let  image = Cell.imageView.image //set picture  imageview.image = Image //set ImageView related properties (stretch mode)  Imageview.contentmode =. Scaleaspectfill //trim redundant portions  imageview.clipstobounds = true  //return picture  return  Imagevie W} 
-3.3 Return the frame value of the final display picture
func photoBrowserRect(indexPath: NSIndexPath) -> CGRect {        //取出cell        letas! HomeViewCell        //取出cell中显示的图片        let image = cell.imageView.image        //计算方法后的图片的frame        return calculate(image!)    }
--3.4 How do I calculate the final image value based on the incoming image value? Answer the following 4 create a class, inside just provide a frame to calculate the display picture (outside can also be called)

--Code in Class 4.1:
import uikit//mark:-Set picture of frame func calculate (image:uiimage)->cgrect {let image Viewx:cgfloat = 0.0  let imagevieww:cgfloat = Uiscreen.mainscreen  () .bounds  .width  Let imageviewh:cgfloat = Imagevieww/image.size  .width  * image.size   Let imageviewy:cgfloat = (Uiscreen.mainscreen  () .bounds  .height -IMAGEVIEWH) * 0.5  return CGRect (x : Imageviewx, y : Imageviewy, Width:imagevieww, HEIGHT:IMAGEVIEWH)} 
Seven protocol (vanishing animation) 1 and pop-up animation the same truth, but this section only need to get the current display picture of Indexpath and ImageView2 clear who can provide this part of the demand; where is the proxy set to? 3 Define protocol: (Implement protocol method definition in transition animation class) )
//MARKclass {    //获取当前显示的图片的角标    func currentIndexPath() ->NSIndexPath    //获取imageView(用这张临时的图片来实现动画效果)    func imageView() ->UIImageView}
4 Setting up proxies
class PhotoBrowserAnimator: NSObject {    //设置消失动画的代理    weakvar dismissDelegate : DismissDelegate?}
5 Vanishing Animation--5.1 getting the current display picture position--5.2 getting imageview--> 5.3 based on the user's swipe after the corner mark, to show the final vanishing animation after the picture in home position (with Presentdelegate proxy)-- 5.4 Code:
//DisappearFunc animatefordismissed (transitioncontext:uiviewcontrollercontexttransitioning) {//1 Remove the Vanishing view         LetDismissview=Transitioncontext.Viewforkey (Uitransitioncontextfromviewkey)!Dismissview.Removefromsuperview ()//2 performing animations        //2.1 Checksum (returns directly if there is no value)Guard LetDismissdelegate=DismissdelegateElse{return} Guard LetPresentdelegate=PresentdelegateElse{return}//2.2 Get a picture         LetImageView=Dismissdelegate.ImageView ()//2.3 Add a picture to a parent controlTransitioncontext.Containerview ()?.Addsubview (ImageView)//2.4 Gets the corner label of the picture that is currently being displayed         LetIndexpath=Dismissdelegate.Currentindexpath ()//2.5 set start positionImageView.Frame=Presentdelegate.Photobrowserrect (Indexpath)//Perform animationsUIView.Animatewithduration (Transitionduration (Transitioncontext), animations: {() -Void inchImageView.Frame=Presentdelegate.Homerect (Indexpath)}) {(_) -Void inch            //Finish animationTransitioncontext.Completetransition (true)        }    }}
6 Setting up the proxy (because creating Photobrowsercontroller is created in the Homeviewcontroller Class)
//Mark: - 点击图片,弹出控制器extension HomeViewController {        //定义为私有的        privateshowPhotoBrowser( indexPath : NSIndexPath) {        //创建控制器对象        let showPhotoBrowserVC = PhotoBrowserController()        //设置dismiss的代理        photoBrowserAnimator.dismissDelegate = showPhotoBrowserVC}
--6.1 make it clear that only the controller that presents the picture can provide these requirements, so set it to proxy 7 to implement the Proxy method

-7.1 Gets the current Indexpath value according to the cell
//Mark: - 实现dismiss的代理方法extension PhotoBrowserController : DismissDelegate {    -> NSIndexPath {        //获取当前正在显示的cell        let= collectionView.visibleCells().first!        //根据cell获取indexPath        return collectionView.indexPathForCell(cell)!    }}    
--7.2 Return a picture (and 7.1 in the same method)
func imageView() -> UIImageView {        //创建UIImageView对象        let imageView = UIImageView()        //获取正在显示的cell        letas! PhotoBrowerViewCell        //取出cell中的图片        imageView.image = cell.imageView.image        //设置图片的属性        imageView.contentMode = .ScaleAspectFill        //将多出的图片裁剪        true        //返回图片        return imageView    }
Eight details processing 1 in the pop-up animation click on the picture in the home through the transition animation into a large image, then you will find that the picture becomes larger in the process of the home background did not disappear, but completely show the big picture to disappear. How to Solve?—-> 1.1 by setting the background color to black when animating, Cover Home
//设置弹出动画的时候背景颜色为黑色        transitionContext.containerView()?.backgroundColor = UIColor.blackColor()
--1.2 When the animation finishes and then the color is set to Clearcolor
transitionContext.containerView()?.backgroundColor = UIColor.clearColor()
2 Do not forget the transition animation after the completion of the need to tell the system transitions animation, or there will be inexplicable situation. 3 Try to verify that the agent has a value, but you can also either force the package (not very secure) or pass the safety point. Nine Summary 1 This part is more difficult, is the enhancement of the previous transition animation. If there are readers who do not understand is also very normal, what problems can be directly to my message, I will be as far as possible to answer. 2 The code may be considered to be not perfect, I just try to finish. Finally, if everyone thinks I'm OK, please follow my official blog, thank you!!!!

Swift Custom transition animations (more difficult)

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.