【问题标题】:How to move a UIView to a new position in iOS?如何将 UIView 移动到 iOS 中的新位置?
【发布时间】:2015-11-25 02:56:54
【问题描述】:

我在主 ViewController 中使用一个视图,并希望通过单击按钮将其移动到新位置。 我已经尝试过使用不同的方法...

.h 文件

@property (nonatomic, strong) IBOutlet UIView *UIview3;

.m 文件

@synthesize UIview3;

我已经实现了以下方法。

方法:1

CGRect f = self.UIview3.frame;
f.origin.x = 28; // new x
f.origin.y = 74; // new y
self.UIview3.frame = f;

方法:2

UIView *newview = [[UIView alloc]initWithFrame:
CGRectMake(28, 74, 261, 119)];
[self.UIview3 addSubview:newview];

其他方法(不在此工作)

UIview3.frame = CGRectMake(28, 231, 261, 119);
UIview3.center = CGPointMake( 28,74);
[_UIview3 setFrame:CGRectMake(28, 74, 261, 119)];
[_UIview3 setFrame:CGPointMake( 28,74)];
[self.UIview3 setCenter:CGPointMake(28, 74)];

有人帮忙吗?提前致谢。 苏哈诺尔

【问题讨论】:

  • 你可以使用 UIAnimation 来获得你需要的效果。
  • 我已经尝试过了,但我需要暂停视图,直到再次按下按钮。 [UIView animateWithDuration:0.9 动画:^{ UIview3.frame = CGRectMake(28, 74, 261, 119); // 最后一帧 }];-> 你能帮我吗?请@你好
  • 你在哪里写的动画块?
  • 检查您是否忘记在.xibstoryboard 设置插座。

标签: ios objective-c iphone uiview


【解决方案1】:

你可以试试这样的:

[UIView animateWithDuration:0.1
        delay:0.1
        options: nil
        animations:^
                  {
                      [UIview3 setFrame:CGRectMake(28, 74, 261, 119)];
                  }
        completion:^(BOOL finished)
                  {

                  }
];

你可以这样设置框架:

[UIview3 setFrame:CGRectMake(28, 74, 261, 119)];

不要使用new 关键字重新分配UIview3,因为它是通过IB 连接的。

【讨论】:

  • 我已经尝试过了。我需要在新位置暂停视图,直到再次按下按钮。 @NeverHopeless
  • I need to pause the view in the new position 是什么意思?没听懂。
  • 我不想使用动画。我想永久移动 UIView 的位置。
【解决方案2】:

在你的 UIButtons 点击事件上试试这些。

[UIView animateWithDuration:0.5 //animation duration..
                      delay:0.2   //animation delay..
                    options: nil
                 animations:^{
                  //Write the coordinates you wants..
                     [_UIview3 setFrame:CGRectMake(28, 74, 261, 119)];
                 }
                 completion:^(BOOL finished){
                     NSLog(@"Done!");

                 }];

希望对你有帮助..

【讨论】:

    【解决方案3】:

    您使用了以下方法,但没有更改您要移动的视图的中心。你把它设置成之前的样子。

    [self.UIview3 setCenter:CGPointMake(28, 74)];
    

    请更改其中心点以移动到按钮的 IBAction 中的新位置,如下所示。

    [self.UIview3 setCenter:CGPointMake(100, 100)];
    

    有关动画目的,请参见 NeverHopeless 发布的上述动画块

    【讨论】:

      【解决方案4】:

      首先,您必须从情节提要检查是否启用了自动布局

      如果未启用自动布局,则可以设置框架并移动视图 BT 如果启用了自动布局,则必须在情节提要中添加适当的约束或将其用于手动约束集

      UIView *newview = [[UIView alloc]initWithFrame:
                         CGRectMake(28, 74, 261, 119)];
      [UIview3 addSubview:newview];
      
      newview.translatesAutoresizingMaskIntoConstraints = NO;
      
      NSLayoutConstraint *topX = [NSLayoutConstraint constraintWithItem:newview
                                                              attribute:NSLayoutAttributeTop
                                                              relatedBy:NSLayoutRelationEqual
                                                                 toItem:UIview3
                                                              attribute:NSLayoutAttributeTop
                                                             multiplier:1
                                                               constant:28];
      
      NSLayoutConstraint *leftY = [NSLayoutConstraint constraintWithItem:newview
                                                               attribute:NSLayoutAttributeLeft
                                                               relatedBy:NSLayoutRelationEqual
                                                                  toItem:UIview3
                                                               attribute:NSLayoutAttributeLeft
                                                              multiplier:1.0
                                                                constant:74];
      [newview addConstraints:@[
                                [NSLayoutConstraint constraintWithItem:newview attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutRelationEqual multiplier:1 constant:261],
                                [NSLayoutConstraint constraintWithItem:newview attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutRelationEqual multiplier:1 constant:119]]];
      
      [UIview3 addConstraints:@[topX,
                                leftY
                                ]];
      

      为了移动新视图,您可以使用此代码

      topX.constant = 50;
      leftY.constant = 100;
      
      [newview updateConstraintsIfNeeded];
      
      [UIView animateWithDuration:0.2 animations:^{
          [newview layoutIfNeeded];
      }];
      

      如果您从情节提要中设置约束,那么您可以简单地创建 x nd y 约束的 IBOutlet,而不是使用上面的代码来移动视图

      【讨论】:

        猜你喜欢
        • 2011-11-05
        • 2014-11-18
        • 1970-01-01
        • 2017-07-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多