【问题标题】:Draggable UIImageViews copies可拖动的 UIImageViews 副本
【发布时间】:2012-11-30 21:03:22
【问题描述】:

我需要制作一个启用了拖放功能的子类 UIImageView 的精确副本,因此我可以拖动它但留下一个副本来替换它,或者只是制作一个我可以拖动到另一个地方而不是第一个地方的副本。

一些截图说明:

拖动前

拖动:

如您所见,我想将纸杯蛋糕留在原处,以便继续从中拖动多个副本。


没关系...我想通了!如何?我在子类 UIImageView 中创建了另一个实例,复制了每个属性(UIImage、delegate、frame)将其添加到同一个超级视图中,并使用协议将其传递给我需要的 UIView...

解决方案:

代码:

#import <UIKit/UIKit.h>
#import "DragAndDrop.h"

@interface DraggableImageView : UIImageView

@property (weak, nonatomic) id <DragAndDrop> delegate;

@property CGPoint startLocation;
@property (strong, nonatomic) DraggableImageView *copied;

@end

#import "DraggableImageView.h"

@implementation DraggableImageView

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    self.copied = [[DraggableImageView alloc] initWithImage:[self image]];

    [self.copied setDelegate:[self delegate]];
    [self.copied setFrame:[self frame]];

    [[self superview] addSubview:self.copied];

    CGPoint pt = [[touches anyObject] locationInView:self.copied];

    self.copied.startLocation = pt;

    [[self.copied superview] bringSubviewToFront:self.copied];
}

【问题讨论】:

    标签: ios objective-c drag-and-drop uiimageview


    【解决方案1】:

    当用户点击纸杯蛋糕时,只需创建一个新的 UIImageView,然后从屏幕上已经存在的那个复制它。

    【讨论】:

      【解决方案2】:

      您需要在该类中实现 NSCopying,这里是另一篇关于如何执行此操作的帖子的链接:another stack overflow post

      在那篇文章中,您将使用的 copywithzone 方法基本上是关于如何制作班级副本的说明。

      在检测用户何时触摸/拖动该图像的代码区域中,使用上述方法对其进行复制,设置其适当的框架 x 和 y 位置,然后将其作为子视图添加到拿着原来的对象。

      //Somtheing like this
      myCustomObject *object2 =[object copy];
      object2.frame = cgrectmake.....
      [view addSubview:object2];
      

      【讨论】:

        猜你喜欢
        • 2017-08-10
        • 1970-01-01
        • 1970-01-01
        • 2011-08-10
        • 1970-01-01
        • 2012-09-21
        • 2012-07-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多