如何通过点击让UIView移动?

时间:2014-06-13 09:49:47

标签: ios objective-c xcode uiview interface-builder

我不确定我是否可以正确描述它。这是一种UIView,我们只看到它的一部分(可能是箭头,可能是屏幕底部)。用户可以单击它,此视图显示为全屏(通常开发人员会留下一些其他信息)。我该怎么做?

enter image description here

3 个答案:

答案 0 :(得分:2)

试试这个:

-(void)show
{
    [viewObj setFrame:CGRectMake(viewObj.frame.origin.x,viewObj.frame.origin.y,viewObj.frame.size.width, viewObj.frame.size.height)];//Here "viewObj.frame.origin.y" means 568
    [UIView animateWithDuration:0.5 animations:^
     {
         [viewObj setFrame:CGRectMake(viewObj.frame.origin.x,0,viewObj.frame.size.width, viewObj.frame.size.height)];
     }
     completion:^(BOOL finished)
     {
     }];
}

-(void)hide
{
    [viewObj setFrame:CGRectMake(viewObj.frame.origin.x,viewObj.frame.origin.y,viewObj.frame.size.width, viewObj.frame.size.height)];//Here "viewObj.frame.origin.y" means 0
    [UIView animateWithDuration:0.5 animations:^
     {
         [viewObj setFrame:CGRectMake(viewObj.frame.origin.x,568,viewObj.frame.size.width, viewObj.frame.size.height)];
     }
     completion:^(BOOL finished)
     {
     }];
}

答案 1 :(得分:1)

我只是设置了视图的Y坐标设置和用户点击Button时的逻辑。动画和重新设置视图框架的Y坐标设置如下:

 yourview.frame = CGRectMake(0,280,320,568);

  [UIView animateWithDuration:2
                              delay:0.1
                            options: UIViewAnimationOptionTransitionFlipFromBottom
                         animations:^{

                            yourview.frame = CGRectMake(0, 0, 320, 568);  
                    }
                     completion:^(BOOL finished){
                   }];

另见这个例子:

https://github.com/crocodella/PullableView

在上面的github演示中,你正在寻找它的输出,如:

enter image description here

答案 2 :(得分:0)

看看UIView参考: https://developer.apple.com/library/ios/documentation/uikit/reference/uiview_class/uiview/uiview.html

具体做法是:

@property(nonatomic) CGPoint center

+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations

示例:

[UIView animateWithDuration:2.0 animations:^{
        myView.center = CGPointMake(self.view.center.x, self.view.center.y + myOffset);
    }];

持续时间以秒为单位。在这种情况下,2秒。

如果您使用的是AutoLayout,则可能需要更改layout constant,而不是center属性。

关于布局约束:

https://developer.apple.com/library/ios/documentation/AppKit/Reference/NSLayoutConstraint_Class/NSLayoutConstraint/NSLayoutConstraint.html

查看constant属性。您还可以从Interface Builder拖动约束插座并访问其constant。一旦你了解它,这真的很简单。