只要用戶按下手指,iOS8 +應用程序中的按鈕應該通過在按鈕周圍繪製輪廓來作出反應。目標是將此行爲封裝到類(cp。類以下的層次結構)中。當釋放手指時,應用程序應該執行定義的動作(主要執行到另一個視圖控制器的繼續)。這裏是我爲了這個目的當前類層次結構:iOS:如何在超類中正確實現自定義手勢識別器?
- UIButton
|_ OutlineButton
|_ FlipButton
的FlipButton類進行一些花哨的翻頁效果,另外我有陰影在UIView的一個類別,圓潤的邊角和輪廓。
目前,我有以下附加類:
#import <UIKit/UIKit.h>
@interface TouchDownGestureRecognizer : UIGestureRecognizer
@end
......與對應的實現:
#import "UIView+Extension.h"
#import "TouchDownGestureRecognizer.h"
@implementation TouchDownGestureRecognizer
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[self.view showOutline]; // this is a function in the UIView category (cp. next code section)
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
[self.view hideOutline]; // this is a function in the UIView category (cp. next code section)
}
@end
...這是UIView的+ Extension.m類別的相關片段用於在按鈕上繪製輪廓:
- (void)showOutline {
self.layer.borderColor = [UIColor whiteColor].CGColor;
self.layer.borderWidth = 1.0f;
}
- (void)hideOutline {
self.layer.borderColor = [UIColor clearColor].CGColor;
}
...和OutlineButton.m文件中我見到目前爲止如下:
#import "OutlineButton.h"
@implementation OutlineButton
- (id)initWithCoder:(NSCoder*)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self addGestureRecognizer:[[TouchDownGestureRecognizer alloc] init]];
}
return self;
}
@end
外觀上看,這款儘快正常工作,作爲一個按鈕被觸摸的輪廓被繪製並儘快釋放手指再次隱藏。但是,通過故事板連接到這些按鈕的IBAction和segues是在經過很長時間(大約2秒)之後纔會執行的。如果多次按下該按鈕(...長時間延遲後),也會執行多次操作。真的很奇怪的行爲...
有人有任何想法如何解決這個問題?
解決方案(基於馬特的回答,謝謝):
#import "OutlineButton.h"
#import "UIView+Extension.h"
@implementation OutlineButton
- (id)initWithCoder:(NSCoder*)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self addTarget:self action:@selector(showOutline) forControlEvents:UIControlEventTouchDown];
[self addTarget:self action:@selector(hideOutline) forControlEvents:UIControlEventTouchUpInside | UIControlEventTouchUpOutside];
}
return self;
}
@end
感謝您的快速響應。如果還有另一種方式比爲輪廓製作圖像,我更喜歡這樣做......您提出的實現方式可能很簡單,但爲每種可能的尺寸管理圖像時,我的按鈕最終都會變得亂七八糟。所以,這個問題中描述的另一種方法將受到高度讚賞。超級類中的自定義手勢識別器是否真的搞砸了響應者鏈? – salocinx
你只是傻了。沒有「圖像」需要被「管理」。看看我的答案中的屏幕錄像。這裏沒有使用「圖像」。該矩形根據按鈕的大小創建並分配在代碼中。 – matt
好吧,我知道了,你的措辭有點尷尬......我根據你的回答,用正確的代碼段擴展了我的問題。謝謝。 – salocinx