UIView拖动(图像和文字) [英] UIView drag (image and text)

查看:93
本文介绍了UIView拖动(图像和文字)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在iOS屏幕上同时拖动UIView同时显示图像和文字?例如小卡片。你能指点我类似的(已解决的)主题吗?我还没有找到任何。

Is it possible to drag UIView around the iOS screen while it has both image and text? e.g. small cards. Could you point me to the similar (solved) topic? I haven't found any.

推荐答案

虽然UIView没有内置支持在用户拖动时移动自己,实施它应该不那么困难。当您只处理拖动视图时,它更容易,而不是其他操作,如点击,双击,多点触摸等。

While UIView does not have a built-in support for moving itself along the user dragging, it should be not so difficult to implement it. It is even easier when you are only dealing with dragging on the view, and not other actions such as tapping, double tapping, multi-touches etc.

要做的第一件事是通过子类化UIView来创建自定义视图,比如 DraggableView 。然后覆盖UIView的 touchesMoved:withEvent:方法,您可以在那里获得当前拖动位置,并移动DraggableView。请看以下示例。

First thing to do is to make a custom view, say DraggableView, by subclassing UIView. Then override UIView's touchesMoved:withEvent: method, and you can get a current dragging location there, and move the DraggableView. Look at the following example.

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *aTouch = [touches anyObject];
    CGPoint location = [aTouch locationInView:self.superview];
    [UIView beginAnimations:@"Dragging A DraggableView" context:nil];
    self.frame = CGRectMake(location.x, location.y, 
                            self.frame.size.width, self.frame.size.height);
    [UIView commitAnimations];
}

并且因为DraggableView对象的所有子视图也将被移动。因此,将所有图像和文本作为DraggableView对象的子视图。

And because all subviews of the DraggableView object will be moved, too. So put all your images and texts as subviews of the DraggableView object.

我在这里实现的内容非常简单。但是,如果您想要更复杂的拖动行为(例如,用户必须点击视图几秒钟才能移动视图),那么您将不得不重写其他事件处理方法(touchesBegan:withEvent:和touchesEnd:withEvent)。

What I implemented here is very simple. However, if you want more complex behaviors for the dragging, (for example, the user have to tap on the view for a few seconds to move the view), then you will have to override other event handling methods (touchesBegan:withEvent: and touchesEnd:withEvent) as well.

这篇关于UIView拖动(图像和文字)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆