0

我在 superview 中有多个视图。如何在不重叠或接触其他视图的情况下拖动 uiimageview。任何帮助表示赞赏..

4

1 回答 1

1

我已经实现了类似的东西。在这里,我发布代码片段。Draggable 是您需要在包含图像的其他类中导入的类。

1) 可拖动.h

#import <UIKit/UIKit.h>

@interface Draggable : UIImageView
{
    CGPoint startLocation;
}
@end

2) 可拖动.m

#import "Draggable.h"

@implementation Draggable

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
    // Retrieve the touch point
    CGPoint pt = [[touches anyObject] locationInView:self];
    startLocation = pt;
    [[self superview] bringSubviewToFront:self];
}
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
    // Move relative to the original touch point
    CGPoint pt = [[touches anyObject] locationInView:self];
    CGRect frame = [self frame];
    frame.origin.x += pt.x - startLocation.x;
    frame.origin.y += pt.y - startLocation.y;
    [self setFrame:frame];
}

@end

3) ProfilePicViewController.m - 我的图片类

#import "Draggable.h"

UIImageView *dragger;

-(void)viewWillAppear:(BOOL)animated
{
    UIImage *tmpImage = [UIImage imageNamed:@"icon.png"];

    CGRect cellRectangle;
    cellRectangle = CGRectMake(0,0,tmpImage.size.width ,tmpImage.size.height );
    dragger = [[Draggable alloc] initWithFrame:cellRectangle];
    [dragger setImage:tmpImage];
    [dragger setUserInteractionEnabled:YES];

    [self.view addSubview:dragger];

}

在这里,您可以在其他图像上拖动“拖动器”。确保具有适当的图像尺寸。icon.png 的大小为 48X48。所以只要有适合你屏幕的图像大小。

希望这可以帮助你。

于 2013-02-01T12:03:01.687 回答