【问题标题】:iOS drag and drop to another view with subclassiOS拖放到另一个带有子类的视图
【发布时间】:2014-02-10 21:53:43
【问题描述】:

我有一个带有 UIScrollView (boardScrollView) 的 RootViewController。这有一个 UIImageView 作为子视图来创建一个板(boardImage)。我可以放大和缩小,工作正常。滚动效果也很好!
现在,我想将其他 UIImageViews (Tiles) 拖放到 ScrollView 中和从中取出。

我将 UIImageView 子类化为 TileImageView 并覆盖了方法 touchesBegan、touchesMoved 和 touchesEnded。拖放在超级视图上工作正常。但我希望能够将 Tiles 放在 ScrollView 上,它是 RootViewController 上的 IBOutlet。

我可以拖放磁贴,因为我可以使用superview,但我不知道如何将磁贴添加到 boardScrollView。

有人知道我如何从子类访问 RootViewController 上的对象吗?
我应该委托一些东西吗?或者设置不同的参数?

代码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:

#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 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....
}

【问题讨论】:

    标签: ios uiscrollview drag-and-drop uiimageview subclass


    【解决方案1】:

    我可以通过遍历子视图将磁贴添加到 ScrollView。

    - (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;
            } 
        } 
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-11
      • 2016-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多