我有RootViewController
UIScrollViewController
(boardScrollView)。
这个boardScrollView
有一个UIImageView
作为子视图(boardImage)来创建电路板。我可以放大和缩小并在boardScrollView
内的boardImage上滚动。效果很棒!
现在我想拖动&将其他UIImageViews
放入boardScrollView中的boardScrollImage以及boardScrollView
的OUT中。
对于这些其他UIImageViews(Tiles),我创建了UIImageView
类(TileViewClass)的子类。
我有拖放工作将Tile INTO放到boardScrollView/boardImage
并拖放到boardScrollView/boardImage
内,但我无法拖放到{OUT}的boardScrollView
工作..
我认为这是因为我无法从子类中访问rootviewcontroller
中的视图。
将图块放回touchesBegan中的顶视图(窗口)可能更好,因此始终从同一视图确定放置位置。 但我不知道如何做到这一点......
我在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;
}
}
答案 0 :(得分:0)
基本上你会捕捉tileimageview上的所有触摸事件并移动它的原点CGPoint。 如果触摸事件结束,您可以骑自行车浏览瓷砖视图的所有子视图。
如果其中任何一个是UIScrollview,您会尝试找到接触点是否与其帧匹配。
所以基本上你只是检查你的uiscrollview的框架。因此,您的接触点不能在它之外。
解决方案:您必须检查要删除tileimageview的视图!你可以假设它是你的uiscrollview的兄弟(我假设)。
其他方式,我建议:创建一个视图 - dropgroundview,在其中捕获触摸(=&gt;实现touchesend) 在那里你可以放弃你的观点并检查例如子类dropviews。
答案 1 :(得分:0)
解决方案:我已将触摸方法委托给RootViewController。在那里,我可以访问所有子视图,最重要的是:最高视图。
我在这里说:
[self.view bringSubviewToFront:self.tmpDragObject];
然后它有效!