如何将幻灯片动画添加到uiview高度常量?

时间:2017-08-03 15:59:29

标签: swift uiswipegesturerecognizer

我试图创建一种效果,当滚动/向下滑动时,视图控制器顶部的UIView将缩小,底部将展开。

这是我添加到上方视图中的滑动手势的代码。

@IBAction func swipeUp(_ sender: UISwipeGestureRecognizer) {
        if(upperView.isHidden == false){
            upperView.isHidden = true
            self.heightUpperView.constant = 0
            self.view.layoutIfNeeded()
        }
        else{
            self.heightUpperView.constant  = 372
            upperView.isHidden = false
            self.view.layoutIfNeeded()
        }
    }

它有效,但缺乏动画。我想知道什么是模仿滚动动画或粘贴标题动画的最佳方法。

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

swift 3.0

你可以尝试下面一个。

import { Component, OnInit } from '@angular/core';
import { Http } from '@angular/http';
import {CribsService} from './../service/cribs.service';
import 'rxjs/add/operator/map';

  @Component({
  selector: 'app-cribs-listing',
  templateUrl: './cribs-listing.component.html',
  styleUrls: ['./cribs-listing.component.css']  
   })

export class CribsListingComponent implements OnInit {

Cribs: Array<any>;
error:String;

constructor(private http:Http,
private cribsservice: CribsService  
) { }

ngOnInit() {

this.cribsservice.getAllCribs()
.subscribe( 
  data => this.Cribs= data,
  error => this.error=error.statusText
);


 }

}

答案 1 :(得分:1)

必须从self.view.layoutIfNeeded()调用

UIView.animateWith...。以下是您的代码示例:

@IBAction func swipeUp(_ sender: UISwipeGestureRecognizer) 
{
     // Set new constants first
     if(upperView.isHidden == false)
     {
          upperView.isHidden = true
          self.heightUpperView.constant = 0
     }
     else
     {
          self.heightUpperView.constant  = 372
          upperView.isHidden = false
     }

     // This line will animate all your constraint changes
     UIView.animate(withDuration: 0.3) 
     {
          self.view.layoutIfNeeded()
     }
}