使用Interface Builder检测UIView上的触摸 [英] Detect touch on UIView with Interface Builder

查看:89
本文介绍了使用Interface Builder检测UIView上的触摸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 UIviewController 中仅检测代码(不使用Interface Builder)检测 UIView / p>

我发现了touchesBegan方法,但它不会被调用。我没有初始化任何其他关于这种方法。

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


解决方案

  #import TouchView.h


//TouchView.h

#import< Foundation / Foundation.h>
#importTouchViewDelegate.h

@interface TouchView:UIView {
id< TouchViewDelegate>代表;
}
@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


How can I detect touches in a UIviewController for a UIView with code only (without Interface Builder)?

I found the touchesBegan method but it doesn't get called ever. I didn't initialize anything else regarding this method.

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

这篇关于使用Interface Builder检测UIView上的触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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