我是iOS开发的新手。

我正在尝试创建Doodle应用,但我没有使用情节提要。我的代码是这样的:


我有一个名为VWDashboard的UIViewController
VWDashboard将UIView添加为名为Doodle的子视图。
Doodle子视图应该是要绘制涂鸦的屏幕,因为Dashboard具有其他视图和其他内容……Dahsboard将“打开”此Doodle视图(将其添加为子视图)。
要绘制涂鸦,我需要在UIView上调用touchesBegan, touchesMoved, touchesEnded方法,但要在VWDashboard上获得它。在VWDashboard中,我有这个:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [doodleDelegate touchBegan:touches withEvent:event];
    NSLog(@"Dashboard: touchesBegan");
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
   [doodleDelegate touchMoved:touches withEvent:event];
    NSLog(@"Dashboard: touchesMoved");
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [doodleDelegate touchEnded:touches withEvent:event];
    NSLog(@"Dashboard: touchesEnded");
}



在我的UIView中,声明了这些方法(以覆盖我的想法),并且我有在此处绘制涂鸦的代码(我在UIViewController上进行了一些演示并工作了)。

当我触摸屏幕时,可以看到所有这些日志,但是不会调用子视图上的方法。

我试图创建一个代表来“侦听” VWDashboard上的方法,并将数据传递给我的UIView Doodle,以便将Doodle绘制在相应的UIImageView中,等等,但这种方法不起作用:

VWDashboard.h

    @class Doodle;
    @protocol DoodleDelegate <NSObject>

    - (void)touchBegan:(NSSet *)touches withEvent:(UIEvent *)event;
    - (void)touchMoved:(NSSet *)touches withEvent:(UIEvent *)event;
    - (void)touchEnded:(NSSet *)touches withEvent:(UIEvent *)event;

    @end

    @interface VWDashboard : VWBase {
        id <DoodleDelegate> delegate;
    }

    @property (retain, nonatomic) id <DoodleDelegate> delegate;

    @end


Doodle.h

    #import <UIKit/UIKit.h>
    #import "VWDashboard.h"

    NS_ASSUME_NONNULL_BEGIN

    @interface Doodle : UIView <DoodleDelegate> {

        // Drawing
        CGPoint lastPoint;
        CGFloat red;
        CGFloat green;
        CGFloat blue;
        CGFloat brush;
        CGFloat opacity;
        BOOL mouseSwiped;
    }

    @end

    NS_ASSUME_NONNULL_END


Doodle.m

    #import "Doodle.h"
    #import "SCLAlertView.h"

    @implementation Doodle {

        UIImageView *canvasSignature, *canvasDoodle;
    }

    - (id)initWithFrame:(CGRect)frame {

    /// renderUI ....

        return self;
    }

    -(void) renderUI...

    - (void)touchBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    // Drawing code

    }
    - (void)touchMoved:(NSSet *)touches withEvent:(UIEvent *)event {
        // Drawing code
    }
    - (void)touchEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    // Drawing code
    }

    @end


这是我在VWDashboard中设置委托的方式:

    Doodle *doodl = [[Doodle alloc] initWithFrame: _moduleLayoutActive.bounds];
    [self setDoodleDelegate:doodl];
    [_moduleLayoutActive addSubview: doodl];


请!我将不胜感激任何帮助,谢谢!

最佳答案

这就是你可以做的...

(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch= [touches anyObject];
    if ([touch view] == yourView)
    {
        //your code
    }

}

(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch= [touches anyObject];
    if ([touch view] == yourView)
    {
        //your code
    }

}

(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch= [touches anyObject];
    if ([touch view] == yourView)
    {
        //your code
    }

}


希望这可以帮助。

10-08 05:23