gpt4 book ai didi

iphone - 使用 Interface Builder 检测 UIView 上的触摸

转载 作者:行者123 更新时间:2023-12-03 20:26:53 26 4
gpt4 key购买 nike

如何仅使用代码(无需 Interface Builder)检测 UIViewControllerUIView 的触摸?

我找到了 TouchBegan 方法,但它从未被调用过。我没有初始化有关此方法的任何其他内容。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

最佳答案

#import "TouchView.h"


//TouchView.h

#import <Foundation/Foundation.h>
#import "TouchViewDelegate.h"

@interface TouchView : UIView {
id <TouchViewDelegate> delegate;
}
@property (retain) id delegate;
@end

//TouchView.m

@implementation TouchView
@synthesize delegate;

-(id) initWithFrame:(CGRect)frame
{
self.userInteractionEnabled = YES;
return self;
}
-(id) initWithCoder:(NSCoder *)aDecoder
{
self.userInteractionEnabled = YES;
return self;
}
-(void) awakeFromNib
{
self.userInteractionEnabled = YES;
}
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[delegate touchDown:self];
}
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[delegate touchUp:self];
}
@end

//TouchViewDelegate.h
#import <UIKit/UIKit.h>


@protocol TouchViewDelegate
-(void) touchDown:(id) sender;
-(void) touchUp:(id)sender;
@end

关于iphone - 使用 Interface Builder 检测 UIView 上的触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1848040/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com