我有一个UITextField,我想在点击时放大它的宽度.我设置了约束,并确保左边的约束优先级低于我试图在右边设置动画的约束.
这是我尝试使用的代码.
// move the input box
UIView.animateWithDuration(10.5, animations: {
self.nameInputConstraint.constant = 8
}, completion: {
(value: Bool) in
println(">>> move const")
})
Run Code Online (Sandbox Code Playgroud)
这有效,但它似乎只是瞬间发生,似乎没有任何动作.我试着设置它10秒钟以确保我没有遗漏任何东西,但我得到了相同的结果.
nameInputConstraint是我控制拖动以从IB连接到我的类的约束的名称.
感谢您的帮助!
我正在使用Swift进行iOS编程,我正在使用此代码移动UITextField,但它不起作用.我keyboardWillShow正确调用该函数,但文本字段不移动.我正在使用autolayout.
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name:UIKeyboardWillShowNotification, object: nil);
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardWillHideNotification, object: nil);
}
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self);
}
func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
//let contentInsets = UIEdgeInsets(top: 0, left: 0, bottom: keyboardSize.height, right: 0)
var frame = self.ChatField.frame
frame.origin.y = frame.origin.y - keyboardSize.height + 167
self.chatField.frame = frame
println("asdasd")
}
}
Run Code Online (Sandbox Code Playgroud) 我试图动画一些视图,以便它们被景观中的巨型键盘阻挡.如果我简单地为框架设置动画,它的效果很好,但是其他人认为这会适得其反,我应该更新NSLayoutConstraints.但是,它们似乎不具有动画效果.有没有人让他们成功地工作?
//heightFromTop is an NSLayoutConstraint referenced from IB
[UIView animateWithDuration:0.25 animations:^{
self.heightFromTop.constant= 550.f;
}];
Run Code Online (Sandbox Code Playgroud)
结果是立即跳到有问题的高度.
我以前从未使用过autolayout约束.我有一个小的新应用程序我正在研究并注意到NIB的观点默认为自动布局.所以,我想我会抓住机会与它一起努力,并试图找出Apple的目标.
第一个挑战:
我需要调整MKMapView的大小,我想将它设置为新位置.如果我按照以前的方式这样做:
[UIView animateWithDuration:1.2f
animations:^{
CGRect theFrame = worldView.frame;
CGRect newFrame = CGRectMake(theFrame.origin.x, theFrame.origin.y, theFrame.size.width, theFrame.size.height - 170);
worldView.frame = newFrame;
}];
Run Code Online (Sandbox Code Playgroud)
...然后,只要兄弟视图得到更新,MKMapView就会"快速"回到原来的高度(在我的情况下,正在更新UISegmentedControl的标题[myUISegmentedControl setTitle:newTitle forSegmentAtIndex:0]).
所以,我觉得我想要做的是从等于父视图的HIGHT到可相对于UISegmentedControl的顶部,它改变的MKMapView的限制被覆盖:V:[MKMapView]-(16)-[UISegmentedControl]
我想要的是缩短MKMapView高度,以便显示地图视图下方的一些控件.为此,我认为我需要将约束从固定的全尺寸视图更改为底部被约束到UISegmentedControl顶部的约束...并且我希望它在视图缩小到新大小时进行动画处理.
怎么会这样呢?
编辑 - 虽然视图底部立即向上移动170,但此动画不是动画:
[UIView animateWithDuration:1.2f
animations:^{
self.nibMapViewConstraint.constant = -170;
}];
Run Code Online (Sandbox Code Playgroud)
并且nibMapViewConstraint在IB中连接到底部垂直空间约束.
在我的设计示例中,我有以下单一视图:

如您所见,它包含一些简单的约束:
我想要实现的是让这个红色/粉红色的视图从顶部"进入".传统上,在一个无约束的世界中,我只是修改内部的框架UIView.animateWithDuration,但是我不确定我是如何在约束世界中做类似的.
为了重申我的问题,如何让我的视图从场景开始并使从顶部飞入的视图动画?
我已经考虑过制作垂直中心约束(并随后调用layoutIfNeeded),但它没有达到预期的效果.
谢谢你的帮助.
如何使用UIViewController容器转换方法使用自动布局:
-(void)transitionFromViewController:(UIViewController *)fromViewController
toViewController:(UIViewController *)toViewController
duration:(NSTimeInterval)duration
options:(UIViewAnimationOptions)options
animations:(void (^)(void))animations
completion:(void (^)(BOOL finished))completion;
Run Code Online (Sandbox Code Playgroud)
传统上,使用Springs/Struts,您可以设置初始帧(在调用此方法之前),并在传递给方法的动画块中设置最终帧.
该方法可以将视图添加到视图层次结构并为您运行动画.
问题是您不能在同一位置(在方法调用之前)添加初始约束,因为视图尚未添加到视图层次结构中.
任何想法我如何使用此方法以及自动布局?
以下是使用Springs/Struts(框架)执行此操作的示例(谢谢cocoanetics) http://www.cocoanetics.com/2012/04/containing-viewcontrollers
- (void)transitionFromViewController:(UIViewController *)fromViewController toViewController:(UIViewController *)toViewController
{
// XXX We can't add constraints here because the view is not yet in the view hierarchy
// animation setup
toViewController.view.frame = _containerView.bounds;
toViewController.view.autoresizingMask = _containerView.autoresizingMask;
// notify
[fromViewController willMoveToParentViewController:nil];
[self addChildViewController:toViewController];
// transition
[self transitionFromViewController:fromViewController
toViewController:toViewController
duration:1.0
options:UIViewAnimationOptionTransitionCurlDown
animations:^{
}
completion:^(BOOL finished) {
[toViewController didMoveToParentViewController:self];
[fromViewController removeFromParentViewController];
}];
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试在选择表视图单元格时执行一些动画.出于某种原因,完成块过早被调用.即使将持续时间设置为10秒,也会立即调用完成块.
[UIView animateWithDuration:10.0 animations:^{
message.frame = newFrame;
} completion:^(BOOL finished) {
NSLog(@"DONE???");
}];
Run Code Online (Sandbox Code Playgroud)
有关为什么会发生这种情况的任何想法?谢谢.
已UITextView插入UITabBarController(在iPhone上)的标签中.
UITextView很多行.发生了什么?键盘UITextView用光标隐藏了一半.无法编辑文本作为结果.
如何解决所有Apple移动设备(具有不同屏幕分辨率)的问题?非常感谢您的帮助!
从这个答案:
这是接受的答案建议为视图更改设置动画:
_addBannerDistanceFromBottomConstraint.constant = 0
UIView.animate(withDuration: 5) {
self.view.layoutIfNeeded()
}
Run Code Online (Sandbox Code Playgroud)
layoutIfNeeded当我们不更改帧时,为什么要打电话.我们正在改变约束,所以(根据这个其他答案)我们不应该调用setNeedsUpdateConstraints吗?
同样,这个备受好评的答案说:
如果稍后发生某些变化会使您的某个约束无效,则应立即删除约束并调用setNeedsUpdateConstraints
我确实尝试过使用它们.使用setNeedsLayout我的视图在左侧正确设置动画
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func animate(_ sender: UIButton) {
UIView.animate(withDuration: 1.8, animations: {
self.centerXConstraint.isActive = !self.centerXConstraint.isActive
self.view.setNeedsLayout()
self.view.layoutIfNeeded()
})
}
@IBOutlet weak var centerYConstraint: NSLayoutConstraint!
@IBOutlet var centerXConstraint: NSLayoutConstraint!
}
Run Code Online (Sandbox Code Playgroud)
但是使用setNeedsUpdateConstraints 不动画,它只是将视图快速向左移动.
import UIKit
class ViewController: UIViewController …Run Code Online (Sandbox Code Playgroud) 我正在为iOS开发一个应用程序,我正在使用带有AutoLayout ON的Storyboard.我的一个视图控制器有一组3个标签,在某些情况下我想让第二个标签消失.
如果我使用setHidden:TRUE方法,标签将变为不可见,但它仍然显然在视图中占用空间.
有人能指出我正确的方向吗?
ios ×8
autolayout ×5
swift ×4
animation ×2
keyboard ×2
xcode6 ×2
cocoa-touch ×1
constraints ×1
iphone ×1
objective-c ×1
uitextfield ×1
uitextview ×1
uiview ×1
xcode ×1