【发布时间】:2014-02-11 13:11:32
【问题描述】:
我有一个RootViewController 和一个UIScrollViewController (boardScrollView)。
这个boardScrollView 有一个UIImageView 作为子视图(boardImage)来创建板。我可以放大和缩小并在 boardScrollView 内的 boardImage 上滚动。效果很好!
现在我想将其他 UIImageViews 拖放到 boardScrollView 内的 boardScrollImage 中,以及 boardScrollView 的 OUT 中。
对于这些其他 UIImageViews (Tiles),我创建了 UIImageView 类 (TileViewClass) 的子类。
我有拖放工作将 Tile 拖放到 boardScrollView/boardImage 并拖放到 boardScrollView/boardImage 内部,但我无法将拖放到 boardScrollView 外部工作..
我认为这是因为我无法从子类访问rootviewcontroller 中的视图。
也许在 touchesBegan 中将 Tile 放回顶视图(窗口)会更好,因此放置位置的确定总是从同一个视图完成。 但我不知道如何做到这一点......
我在 touchesBegan 方法中尝试过[[UIApplication sharedApplication].keyWindow bringSubviewToFront:self.dragObject];,但这并不能解决问题....
也许我在某处遗漏了 removeFromSuperView?
有人知道如何让拖放工作吗?
RootViewController.h:
@interface RootViewController : UIViewController <UIScrollViewDelegate>
@property (nonatomic, strong) IBOutlet UIScrollView *boardScrollView;
@property (nonatomic, strong) UIImageView *dragObject;
@property (nonatomic, assign) CGPoint touchOffset;
@property (nonatomic, assign) CGPoint homePosition;
@property (nonatomic, strong) UIImageView *boardImage;
@end
RootViewController.m:
@implementation RootViewController
@synthesize boardScrollView;
@synthesize dragObject;
@synthesize touchOffset;
@synthesize homePosition;
@synthesize boardImage;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
UIImage *image = [UIImage imageNamed:@"greyblue_numbered_15x15_900x900.png"];
self.boardImage = [[UIImageView alloc] initWithImage:image];
self.boardImage.frame = (CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=image.size};
[self.boardScrollView addSubview:self.boardImage];
self.boardImage.userInteractionEnabled = YES;
self.boardScrollView.contentSize = image.size;
self.boardScrollView.canCancelContentTouches = NO;
self.boardScrollView.userInteractionEnabled = YES;
self.boardScrollView.clipsToBounds = YES;
}
TileImageView.h:
#import <UIKit/UIKit.h>
@interface TileImageView : UIImageView
@property (nonatomic, strong) UIImageView *dragObject;
@property (nonatomic, assign) CGPoint touchOffset;
@end
TileImageView.m:
#import "TileImageView.h"
@implementation TileImageView
@synthesize dragObject;
@synthesize touchOffset;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.exclusiveTouch = YES;
self.userInteractionEnabled = YES;
}
return self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if ([touches count] == 1) {
// one finger
CGPoint touchPoint = [[touches anyObject] locationInView:self.superview];
for (UIImageView *iView in self.superview.subviews) {
if ([iView isMemberOfClass:[TileImageView class]]) {
if (touchPoint.x > iView.frame.origin.x &&
touchPoint.x < iView.frame.origin.x + iView.frame.size.width &&
touchPoint.y > iView.frame.origin.y &&
touchPoint.y < iView.frame.origin.y + iView.frame.size.height)
{
self.dragObject = iView;
self.touchOffset = CGPointMake(touchPoint.x - iView.frame.origin.x,
touchPoint.y - iView.frame.origin.y);
[self.superview bringSubviewToFront:self];
}
}
}
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint touchPoint = [[touches anyObject] locationInView:self.superview];
CGRect newDragObjectFrame = CGRectMake(touchPoint.x - touchOffset.x,
touchPoint.y - touchOffset.y,
self.dragObject.frame.size.width,
self.dragObject.frame.size.height);
self.dragObject.frame = newDragObjectFrame;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UIView *iView in self.superview.subviews) {
if ([iView isMemberOfClass:[UIScrollView class]])
{
CGPoint touchPointScreen = [[touches anyObject] locationInView:[UIApplication sharedApplication].keyWindow];
if (touchPointScreen.x > iView.frame.origin.x &&
touchPointScreen.x < iView.frame.origin.x + iView.frame.size.width &&
touchPointScreen.y > iView.frame.origin.y &&
touchPointScreen.y < iView.frame.origin.y + iView.frame.size.height)
{
for (UIView *iView2 in iView.subviews) {
[iView2 addSubview:self.dragObject];
CGPoint touchPointImage = [[touches anyObject] locationInView:iView2];
self.dragObject.frame = CGRectMake(touchPointImage.x - touchOffset.x,
touchPointImage.y - touchOffset.y,
self.dragObject.frame.size.width,
self.dragObject.frame.size.height);
}
}
self.dragObject = nil;
}
}
【问题讨论】:
标签: ios drag-and-drop uiimageview