ios - 在 UIView(子类)而不是父 UIViewController 中监听方法(touchesBegan、touchesMoved、touchesEnded)?
问题描述
我是 iOS 开发的新手。
我正在尝试创建一个涂鸦应用程序,我没有使用故事板。我的代码是这样的:
我有一个
UIViewController
名为VWdashboard。VWdashboard添加一个
UIView
名为Doodle的子视图。涂鸦子视图应该是要绘制涂鸦的屏幕,因为仪表板还有其他视图和东西...... Dahsboard 将“打开”这个涂鸦视图(将其添加为子视图)。
要绘制涂鸦,我需要
touchesBegan, touchesMoved, touchesEnded
在我的 UIView 上调用方法,但我在我的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 的委托,以便将在相应的 doodleUIImageView
等中绘制涂鸦,但不起作用:
VW仪表板.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
涂鸦.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
涂鸦.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
}
}
希望这可以帮助。
推荐阅读
- android - 颤振:摇篮构建失败
- jsp - java.lang.UnsupportedOperationException:Servlet 2.x 容器不支持异步处理
- combinations - 从可能的对数中获取原始数组的大小
- debian - 用于多个发行版的 Bintray Debian 存储库
- java - 权威信息来源:最新的 WildFly 版本支持哪个 JDK 版本?
- c++ - 为什么 Visual Studio (2017; 15.7.5) 不让我更改 c++ 关键字颜色
- python - 如何在系列上获取熊猫应用功能的行索引
- mysql - 选择 ID 在作为属性存储的 ID 列表中的记录
- python - Python - 将 AAF 转换为 XML 并读取所用音频的元数据
- javascript - 如何处理从开发环境到生产环境的 Ajax URL 更改?