zoukankan      html  css  js  c++  java
  • AutoLayout And Animation

    1,在rootView中添加一个view,并设置约束,左、右、上,都设置与super的边距为20,高度为100。

    2,编写代码控制

     1 #import "ViewController.h"
     2 
     3 @interface ViewController ()
     4 @property (weak, nonatomic) IBOutlet UIView *animationView;
     5 
     6 @property (nonatomic, assign) BOOL isForwadAnimation;
     7 @end
     8 
     9 @implementation ViewController
    10 
    11 - (void)viewDidLoad {
    12     [super viewDidLoad];
    13     // Do any additional setup after loading the view, typically from a nib.
    14     self.isForwadAnimation = NO;
    15 }
    16 
    17 // 点击view时改变animationView约束中与Top的间距
    18 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    19 {
    20     if(self.isForwadAnimation)
    21     {
    22         [self replaceTopConstrainOnView:self.animationView contant:20];
    23     }
    24     else
    25     {
    26         [self replaceTopConstrainOnView:self.animationView contant:200];
    27     }
    28     // 启动动画
    29     [self animateConstraints];
    30     self.isForwadAnimation = !self.isForwadAnimation;
    31 }
    32 
    33 // 改变约束的值
    34 - (void)replaceTopConstrainOnView:(UIView *)view contant:(float)contant
    35 {
    36     [self.view.constraints enumerateObjectsUsingBlock:^(NSLayoutConstraint *constraint, NSUInteger idx, BOOL *stop) {
    37         if(constraint.firstItem == view && constraint.firstAttribute == NSLayoutAttributeTop)
    38         {
    39             constraint.constant = contant;
    40         }
    41     }];
    42 }
    43 
    44 // 利用UIView的类方法做动画
    45 - (void)animateConstraints
    46 {
    47     [UIView animateWithDuration:2.0 animations:^{
    48         [self.view layoutIfNeeded];
    49     }];
    50 }

     3,总结

    // 如果约束是相对顶部的话就获取firstItem
    // 如果约束是相对底部的话就获取secondItem

  • 相关阅读:
    回到顶部
    angularjs 复选框 单选框
    关于angularjs的ng-repeat指令
    JS字符串对象
    JS的控制语句与异常
    JS的运算符
    JS的引入方式和基础规范
    z-index及透明度opacity,利用overflow设置头像
    css的定位
    css的float属性及清除浮动
  • 原文地址:https://www.cnblogs.com/fkunlam/p/4414676.html
Copyright © 2011-2022 走看看