iOS拖放到带有子类的另一个视图 [英] iOS drag and drop to another view with subclass

查看:58
本文介绍了iOS拖放到带有子类的另一个视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有UIScrollView(boardScrollView)的RootViewController.它具有UIImageView作为创建板(boardImage)的子视图.我可以放大和缩小,效果很好.滚动也可以!!
现在,我要拖动&将其他UIImageViews(标题)拖放到ScrollView中.

I have a RootViewController with a UIScrollView (boardScrollView). This has a UIImageView as subview to create a board (boardImage). I can zoom in and out, works fine. Also scrolling works fine!
Now, I want to drag & drop other UIImageViews (Tiles) into and out of the ScrollView.

我将UIImageView子类化为TileImageView,并且重写了方法touchesBegan,touchesMoved和touchesEnded.拖曳drop在超级视图上工作正常.但是我希望能够将Tiles放到ScrollView上,后者是RootViewController上的IBOutlet.

I have subclassed UIImageView to TileImageView and overridden methods touchesBegan, touchesMoved and touchesEnded. The drag & drop works fine on the superview. But I want to be able to drop the Tiles on the ScrollView which is a IBOutlet on the RootViewController.

我可以拖放图块,因为可以使用 superview ,但是我不知道如何将图块添加到boardScrollView.

I can drag and drop the tile because I can use superview but I don't know how I can add the Tile to the boardScrollView.

任何人都知道如何从子类访问RootViewController上的对象吗?
我应该委派些东西吗?或设置其他参数?

Anybody knows how I can access objects on the RootViewController from the subclass?
Am I supposed to delegate something? Or set a different parameter?

代码RootViewController:

Code RootViewController:

@implementation RootViewController_iphone

@synthesize boardScrollView;
@synthesize dragObject;
@synthesize boardImage;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.boardScrollView.clipsToBounds = YES;

    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.userInteractionEnabled = YES;
    self.boardScrollView.canCancelContentTouches = NO;
}

代码子类TileImageView.h:

Code subclass TileImageView.h:

#import <UIKit/UIKit.h>

@interface TileImageView : UIImageView
@property (nonatomic, strong) UIImageView *dragObject;
@property (nonatomic, assign) CGPoint touchOffset;

@end

代码子类TileImageView.m:

Code subclass 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 now, I use the superview, but to be able to drag from the ScrollView, 
           I need to access the boardScrollView on the UIViewController...
        // i.e     CGPoint touchPoint = [[touches anyObject] locationInView:self.boardScrollView];

        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.dragObject];
                }
            }
        }
    }
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint touchPoint = [[touches anyObject] locationInView:self.superview];
    // here I use the superview, but I also want to use the ScrollView

    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
{
    // here I need to be able to use the ScrollView on the RootViewController....
}

推荐答案

我可以通过在子视图上循环来将图块添加到ScrollView中.

I could add the tile to the ScrollView by looping over the subviews.

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"Ended Tile!");
    for (UIView *iView in self.superview.subviews) {
        NSLog(@"superview.subviews");
        if ([iView isMemberOfClass:[UIScrollView class]])
        {

            CGPoint touchPointScreen = [[touches anyObject] locationInView:self.superview];
            CGPoint touchPointBoard = [[touches anyObject] locationInView:iView];
            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拖放到带有子类的另一个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆