[英]How to convert MASConstraint to NSLayoutConstraint?
我目前在我的應用程序中使用“Masonry”和“POP”。 我想知道如何動畫砌體創建的約束? 所以這是我的代碼:
[imageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.equalTo([NSValue valueWithCGSize:CGSizeMake(ScreenWidth, ScreenHeight)]);
make.top.equalTo(self.mas_top);
}];
所以這是一個簡單的例子。 當POP動畫更改約束時,它需要這樣的東西(我在這里保存一些代碼):
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *containerHeightConstraint;
[self.containerHeightConstraint pop_addAnimation:spring forKey:@"spring"];
我沒有問題讓這兩個庫單獨工作,但我想知道如何動畫MASConstraint的更改,因為POP需要采用NSLayoutConstraint作為參數,如文檔中所述。 那么這里的問題是如何將MASConstraint轉換為NSLayoutConstraint? 有人可以幫忙嗎?
MASConstraint
是一個抽象類,可以同時代表一個而不是幾個NSLayoutConstraints
,所以它不太適用於獲得一個人們期望的引用。
話雖這么說,你可以使用MASConstraint的setOffset
:方法來修改底層NSLayoutConstraint的布局常量,它滿足了許多動畫用例。 如下所示:
MASConstraint *widthConstraint;
[POPSpringAnimation animationWithPropertyNamed:@"offset"];
animation.springBounciness = 8;
[widthConstraint pop_addAnimation:animation forKey:@"myAnimation"];
正在搜索任何替代方案,我遇到了這個帖子 ,作者本人對類似問題的回復。
請參閱github上的“MSSPopMasonry”
lib使用Masonry和Facebook Pop動畫框架
有一個很好的工具,結合了pop和Masonry,稱為MSSPopMasonry: https : //github.com/miklselsoe/MSSPopMasonry 。 使用它(只有2個源文件),您可以在MASContraint上制作流行動畫,因此您無需將MASContraint轉換為砌體不支持的NSLayoutConstraint。 用法很簡單:
// declare the constraint you want to animate with
MASConstraint *_signInScreenLeadingConstraint;
BOOL _isInSignUpScreen = NO;
// set constraint using Masonry
[view mas_makeConstraints:^(MASConstraintMaker *make) {
_signInScreenLeadingConstraint = make.left.equalTo(view.superview).with.offset(0);
make.top.and.bottom.equalTo(view.superview).with.offset(0);
make.width.equalTo(view.superview).with.offset(0);
}];
// animate by pop
POPBasicAnimation *exchangeAnimation = [POPBasicAnimation easeInAnimation];
exchangeAnimation.property = [POPAnimatableProperty mas_offsetProperty];
exchangeAnimation.duration = 0.2f;
exchangeAnimation.toValue = _isInSignUpScreen ? @(0) : @(200);
[exchangeAnimation setCompletionBlock:^(POPAnimation *anim, BOOL finished) {
_isInSignUpScreen = !_isInSignUpScreen;
}];
[_signInScreenLeadingConstraint pop_addAnimation:exchangeAnimation forKey:@"animation"];
感謝MSSPopMasonry的作者:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.