如何将UIView移动到iOS中的新位置?

时间:2015-08-31 06:24:16

标签: ios objective-c iphone uiview

我在主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)];

有人帮忙吗?提前致谢。 sohanoor

4 个答案:

答案 0 :(得分:2)

您可以尝试这样的事情:

[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连接的。

答案 1 :(得分:0)

在您的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!");

                 }];

我希望它有所帮助......

答案 2 :(得分:0)

您使用了以下方法,但未更改要移动的视图的中心。你可以像以前一样设置它。

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

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

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

出于动画目的,请参阅NeverHopeless发布的上述动画块

答案 3 :(得分:0)

首先,你必须从故事板

检查autolayout是否启用

enter image description here

如果autolayout未启用,则可以设置框架并移动视图 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
                          ]];

要移动newview,您可以使用此代码

topX.constant = 50;
leftY.constant = 100;

[newview updateConstraintsIfNeeded];

[UIView animateWithDuration:0.2 animations:^{
    [newview layoutIfNeeded];
}];

如果你从故事板设置约束,你可以简单地创建你的x n和y约束的IBOutlet,而不是使用上面的代码来移动视图