我有一個觀點,我想讓用戶能夠快速切換。我曾嘗試使用UITapGestureRecognizer,但它並沒有像我想的那樣很好地拾取水龍頭。我還有其他方法可以嘗試嗎?獲得UIView響應快速,快速單擊的最佳方式是什麼?
謝謝!
我有一個觀點,我想讓用戶能夠快速切換。我曾嘗試使用UITapGestureRecognizer,但它並沒有像我想的那樣很好地拾取水龍頭。我還有其他方法可以嘗試嗎?獲得UIView響應快速,快速單擊的最佳方式是什麼?
謝謝!
您可以創建自己的UIView派生類,並將其作爲您用於直接獲取「觸摸」輸入的任何視圖的子視圖插入。我用它作爲屏幕上的疊加層,以便我可以觸發屏幕更改,動畫,「我正在查看此輸入機制」等。
此方法使用UIView的touchesEnded覆蓋(請參閱代碼),而不是UITapGestureRecognizer。這是「舊學校」的目標-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稱爲六件事情(you can find it here)。
這裏是選擇器功能(不帶參數):
-(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
對您有幫助嗎?
或者,你可以去UIResponder方法(觸摸方法)像觸摸開始... – Hooda