gpt4 book ai didi

ios - 让 UIView 响应快速、快速的单击的最佳方法是什么?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:08:47 28 4
gpt4 key购买 nike

我的观点是希望用户能够快速切换。我已经尝试使用 UITapGestureRecognizer,但它并没有像我想要的那样很好地接收水龙头。我应该尝试其他方法吗?

谢谢!

最佳答案

您可以创建自己的 UIView 派生类,并将其作为您使用的任何 View 的 subview 插入,以直接获取“触摸”输入。我将它用作屏幕上的叠加层,以便我可以触发屏幕变化、动画、“我已经看完了”输入机制等。

此方法使用 UIView 的 touchesEnded 覆盖(参见代码),而不是 UITapGestureRecognizer。这是“老派”objective-C,不使用 ARC。

这里是头文件“TapView.h”

#import <UIKit/UIKit.h>


@interface TapView : UIView
{
SEL touchedCallback;
id touchedCallbackTarget;
UILabel* label;
}

@property(nonatomic,assign) SEL touchedCallback;
@property(nonatomic,retain) id touchedCallbackTarget;
@property(nonatomic,retain) UILabel* label;

-(void)respondToTap:(SEL)withCallback andTarget:(id)target;

@end

您创建类并调用 [tapView respondToTap....] 以使用您的目标对其进行初始化。像这样的东西(在 viewDidLoad 中):

- (void)viewDidLoad
{
[super viewDidLoad];
UIView* navView = [ViewUtilities SetNavigationTitle:@"Thing Tank" andSubtitle:@"Tap Here To Empty Tank" forView:self];
CGRect frame = navView.frame;
CGRect tapFrame = CGRectMake(0, 0, frame.size.width, frame.size.height);
TapView* tapView = [[[TapView alloc] initWithFrame:tapFrame] autorelease];
[navView addSubview:tapView];
[tapView respondToTap:@selector(tapNavBar) andTarget:self];
self._tapView = tapView;
}

此代码用于允许用户在我为 iPhone 编写的名为 Six Things (you can find it here) 的应用程序中“重置”“Thing Tank”(思考鱼缸)。

这是选择器函数(不带参数):

-(void)tapNavBar
{
[[NSNotificationCenter defaultCenter] postNotificationName:[Constants NOTIFICATION_RESTART_BACKGROUND] object:nil];
}

这是实现,TapView.m。

#import "TapView.h"


@implementation TapView

@synthesize touchedCallback;
@synthesize touchedCallbackTarget;
@synthesize label;

-(TapView*)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if(self)
{
self.touchedCallback = nil;
self.userInteractionEnabled = YES;

UILabel *lbl = [[UILabel alloc] init];
lbl.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
lbl.backgroundColor = [UIColor clearColor];
lbl.textColor = [UIColor whiteColor];
lbl.textAlignment = UITextAlignmentCenter;
lbl.font = [UIFont boldSystemFontOfSize:16];
lbl.text = nil;
lbl.adjustsFontSizeToFitWidth = YES;
self.label = lbl;
[self addSubview:lbl];
[lbl release];
}
return self;
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code.
}
*/

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if(self.touchedCallback != nil && self.touchedCallback != nil)
[self.touchedCallbackTarget performSelector:self.touchedCallback];
}

-(void)respondToTap:(SEL)withCallback andTarget:(id)target
{
self.touchedCallback = withCallback;
self.touchedCallbackTarget = target;
}


- (void)dealloc
{
self.label = nil;
[super dealloc];
}


@end

这有帮助吗?

关于ios - 让 UIView 响应快速、快速的单击的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20723957/

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