我有一个带UIScrollViewController
的RootViewController
(boardScrollView)。这个boardScrollView
有一个UIImageView
作为子视图(boardImage)来创建板。我可以放大、缩小和滚动boardScrollView
中的板图像。效果很好!
现在我想拖动&将其他UIImageViews
放入boardScrollView内的boardScrllImage中,也从boardScrollView
中取出。对于这些其他UIImageViews(Tiles),我创建了UIImageView
类(TileViewClass)的子类。
我有阻力&drop working将Tile放入CCD_ 9;降落在CCD_ 10内部,但我无法获得阻力&下降到boardScrollView
工作的外部。。我想这是因为我无法从子类访问rootviewcontroller
中的视图。
也许在触摸Began中将Tile放回俯视图(窗口)更好,因此放置位置的确定总是从同一个视图中完成。但我不知道该怎么做。。。
我已经在touchesBegan方法中尝试过[[UIApplication sharedApplication].keyWindow bringSubviewToFront:self.dragObject];
,但这并不能奏效。。。。也许我在某个地方错过了SuperView的remove?
任何人都知道我如何才能得到拖动&放弃工作?
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;
}
}
基本上,您可以捕捉tileimageview上的所有触摸事件,并移动其原点CGPoint。如果触摸事件结束,您的自行车将通过tileviews超级视图的所有子视图。
如果其中任何一个是UIScrollview,则尝试定位接触点是否与其边框匹配。
所以基本上你只是检查一下你的用户视图的框架。因此,你的接触点不能在它之外。
解决方案:您必须检查要删除tileimageview的视图!你可以假设它是你的uiscrollview的兄弟(我假设的)。
另一种方式,我建议:创建一个View-dropgroundview,在其中捕捉触摸(=>实现触摸发送)在那里,你可以放下你的视图,并检查例如dropviews的子类。
解决方案:我已将触摸方法委托给RootViewController。在那里,我可以访问所有子视图,最重要的是:最高视图。
这里,我说:
[self.view bringSubviewToFront:self.tmpDragObject];
然后它就工作了!