- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
当我在没有垂直和水平居中的情况下对 UIImageView 和父 UIView 顶部之间的距离使用约束时,动画本身会起作用。
UIImageView 的约束:
当我使用它时动画效果很好:
self.logoImageViewVerticalSpaceConstraint.constant = 20;
[UIView animateWithDuration: 1.0
delay: 0
options:(UIViewAnimationOptionCurveEaseInOut|UIViewAnimationOptionAllowUserInteraction)
animations:^{[self.logoImageView layoutIfNeeded];}
completion:^(BOOL finished) {NSLog(@"Finished animation"); }];
当我不想使用静态空间到顶部布局约束时,问题就出现了,因为 imageview 需要在 3.5 和 4 英寸设备上居中。为了解决这个问题,我想从垂直和水平中心约束开始。
现在我想我可以简单地删除 y 中心约束并自己将空间添加到 20pt 的顶部布局约束:
[self.logoImageView removeConstraint:self.logoImageViewYCenterConstraint];
NSLayoutConstraint *topSpaceConstraint = [NSLayoutConstraint
constraintWithItem:self.view attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual toItem:self.logoImageView
attribute:NSLayoutAttributeTop multiplier:1
constant:20];
[self.view addConstraint:topSpaceConstraint];
[UIView animateWithDuration: 1.0
delay: 0
options:(UIViewAnimationOptionCurveEaseInOut|UIViewAnimationOptionAllowUserInteraction)
animations:^{
[self.logoImageView layoutIfNeeded];
}
completion:^(BOOL finished) {
NSLog(@"Finished animate");
}];
结果是 UIImageView 保持居中并且有点膨胀。这与我期望发生的情况完全相反,因为我删除了 x 中心约束。将距离为 20pt 的顶部约束添加到父 UIView 的顶部,我没有触及任何其他约束(如宽度和高度)。
最佳答案
一个 NSLayoutConstraint
有一个或两个它约束的 View 。在您的例子中, View 是 self.logoImageView
和 self.view
。
要产生任何效果,约束必须安装在 View 上,而不仅仅是任何 View 。约束必须安装在两个受约束 View 的共同祖先上。 (请注意, View 被视为其自身的祖先。)当您删除约束时,您必须将其从安装它的 View 中删除。
您正在尝试从 self.logoImageView
中删除居中约束,但无法在该 View 上安装该约束。
从 iOS 8.0 开始,卸载约束的首选(也是最简单)方法是将其 active
属性设置为 NO
:
self.logoImageViewYCenterConstraint.active = NO;
在 iOS 8.0 之前,您必须将其从安装它的 View 中删除。约束可能安装在 self.view
上。所以试试这个:
[self.view removeConstraint:self.logoImageViewYCenterConstraint];
另请注意,如果您想将 ImageView 动画化回中心,您需要卸载 topSpaceConstraint
并重新安装 self.logoImageViewYCenterConstraint
。
处理此问题的另一种方法是同时安装两个 约束,但给予其中一个较低的优先级。当你需要改变 ImageView 的位置时,改变约束的优先级。因此:
- (void)viewDidLoad {
[super viewDidLoad];
if (self.topSpaceConstraint == nil) {
self.topSpaceConstraint = [NSLayoutConstraint constraintWithItem:...];
self.topSpaceConstraint.priority = 1;
[self.view addConstraint:self.topSpaceConstraint];
}
}
- (void)setImageViewCenteredVertically:(BOOL)isCentered animated:(BOOL)animated {
if (isCentered) {
self.topSpaceConstraint.priority = 1;
self.logoImageViewYCenterConstraint.priority = UILayoutPriorityRequired;
} else {
self.topSpaceConstraint.priority = UILayoutPriorityRequired;
self.logoImageViewYCenterConstraint.priority = 1;
}
if (animated) {
[UIView animateWithDuration:1 delay:0
options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction
animations:^{
[self.view layoutIfNeeded];
}
completion:nil];
}
}
关于ios - 使用 AutoLayout 将 UIImageView 从中心动画化到距父 UIView 顶部 20 点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18112769/
这个问题已经有答案了: 已关闭11 年前。 Possible Duplicate: How to align a to the middle of the page 这是一个简单的问题,应该有一个简
我想知道如何在屏幕中央的ListView中生成2个itens。 class _CategoriesState extends State { List categories = ["Anterio
我需要修复 UICollectionView 的一个小问题,当在 6s 设备尺寸上时,我得到如下布局: 但是,我想知道将它们居中以使其成为两条线的最佳方法是什么,或者我应该将它们缩小一点以便可以放置其
我尝试在 UIImageView 中居中 CAShapeLayer 但没有成功,我找到了解决方案,将其(CAShapeLayer)添加到 UIVIew,然后将 UIVIew 添加到 ImageView
一直在从线性布局和相对布局切换,因为我想实现图像的居中。 显示图片:
* { margin:0; padding:0; } /** General Style Info **/ body { background: #003d4c; co
我目前正在尝试修改我在 Wordpress 上的 Royal Slider 插件 (http://dimsemenov.com/plugins/royal-slider/) 我的目标是将所有标题的文本
我已经坚持了一段时间,因此简化了我的要求。当您单击标记时,信息窗口将打开,当用户缩放时,我希望该标记位于 map 的中心。这不起作用,但我认为它很接近: function bindInfoWindow
/* Linked Styles */ body { padding: 0 !important;
这个问题在这里已经有了答案: How do I center floated elements? (12 个答案) 关闭 7 年前。
这个问题在这里已经有了答案: Is there an equivalent to background-size: cover and contain for image elements? (1
我试图让我的页眉停留在页面的中间,不管我在什么窗口大小。 我试过使用 Bootstrap。 StackOverflow 还有这个 CSS。 .section-t
我有一个 1600 像素宽的页面。主要区域虽然只有 900 像素宽。我有一个导航应该固定在页面的中心(它是)。我的问题是当我打开页面时,页面固定在左侧而不是在打开时居中。当用户访问该网站时,我需要做什
我正在尝试找到 View 的中心。对于非旋转 View ,该值是正确的,但对于旋转 View ,它不正确,因为中心 (0,0) 正在旋转。在缩放的情况下,即使缩放后宽度和高度也保持不变 我正在使用以下
预期效果是将 Kartennummer 和 Passwort 集中。 这怎么可能? 我为此使用了一个自定义类: import 'package:flutter/material.dart'; impo
我管理着许多 Maven 项目。他们中的大多数部署到我们的内部 maven 存储库。现在我想开始向 Maven Central 发布一个项目。到目前为止,我有一个父 POM,它指定了我们内部存储库的
我试图锚定两个进展,但我看不到获得预期结果的方法。 我希望左边的进度条固定在左边,右边的条固定在中心固定(如下图所示) (下图显示调整大小的表格) 我尝试将控件放在具有各种 anchor 的停靠面板上
我正在玩 Angular 模态 ui 对话框。我想知道有什么方法可以让它居中?我发现了一个类似的问题: Twitter Bootstrap - Center Modal Dialog 但无法使其工作,
是否可以将值标签放置在条形上,使它们位于条形的中心? 如果我设置条形 align: "center",则条形上的标签将关闭且不居中。 $(function() { var d
http://www.asp.net/signalr/overview/signalr-20/getting-started-with-signalr-20/tutorial-signalr-20-s
我是一名优秀的程序员,十分优秀!