I am following Apple's WWDC 2010 video on TVAnimationGestures (Advanced Table Views). They expand and collapse a table with a button as a subview of the header. Then later on in their lecture, they add a tap gesture to the whole view. I'd like to have that mechanism in place, but also have another button on the header that has a different action associated with it. However, in debug mode, it seems like whenever I press the header, even in the area where the button is that they created, when I get to the action method, the sender is always a UITapGestureRecognizer. So how would I add another button to get a new action, or in their code, distinguish between the button and the tap of the header. I copied and pasted their code below for that class.
我在TVAnimationGestures(高级表格视图)上关注Apple的WWDC 2010视频。它们使用按钮作为标题的子视图展开和折叠表。然后在他们的演讲中,他们为整个视图添加了一个轻击手势。我希望有这个机制,但在标题上还有另一个按钮,它有一个与之关联的不同动作。但是,在调试模式下,似乎每当我按下标题时,即使在他们创建按钮的区域,当我到达动作方法时,发送者始终是UITapGestureRecognizer。那么我如何添加另一个按钮来获取新动作,或者在代码中如何区分按钮和标题的点击。我在下面复制并粘贴了他们的代码。
/*
File: SectionHeaderView.m
Abstract: A view to display a section header, and support opening and closing a section.
Version: 1.1
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
Inc. ("Apple") in consideration of your agreement to the following
terms, and your use, installation, modification or redistribution of
this Apple software constitutes acceptance of these terms. If you do
not agree with these terms, please do not use, install, modify or
redistribute this Apple software.
In consideration of your agreement to abide by the following terms, and
subject to these terms, Apple grants you a personal, non-exclusive
license, under Apple's copyrights in this original Apple software (the
"Apple Software"), to use, reproduce, modify and redistribute the Apple
Software, with or without modifications, in source and/or binary forms;
provided that if you redistribute the Apple Software in its entirety and
without modifications, you must retain this notice and the following
text and disclaimers in all such redistributions of the Apple Software.
Neither the name, trademarks, service marks or logos of Apple Inc. may
be used to endorse or promote products derived from the Apple Software
without specific prior written permission from Apple. Except as
expressly stated in this notice, no other rights or licenses, express or
implied, are granted by Apple herein, including but not limited to any
patent rights that may be infringed by your derivative works or by other
works in which the Apple Software may be incorporated.
The Apple Software is provided by Apple on an "AS IS" basis. APPLE
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
Copyright (C) 2010 Apple Inc. All Rights Reserved.
*/
#import "SectionHeaderView.h"
#import <QuartzCore/QuartzCore.h>
@implementation SectionHeaderView
@synthesize titleLabel, disclosureButton, delegate, section;
+ (Class)layerClass {
return [CAGradientLayer class];
}
-(id)initWithFrame:(CGRect)frame title:(NSString*)title section:(NSInteger)sectionNumber delegate:(id <SectionHeaderViewDelegate>)aDelegate {
self = [super initWithFrame:frame];
if (self != nil) {
// Set up the tap gesture recognizer.
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toggleOpen:)];
[self addGestureRecognizer:tapGesture];
[tapGesture release];
delegate = aDelegate;
self.userInteractionEnabled = YES;
// Create and configure the title label.
section = sectionNumber;
CGRect titleLabelFrame = self.bounds;
titleLabelFrame.origin.x += 35.0;
titleLabelFrame.size.width -= 35.0;
CGRectInset(titleLabelFrame, 0.0, 5.0);
titleLabel = [[UILabel alloc] initWithFrame:titleLabelFrame];
titleLabel.text = title;
titleLabel.font = [UIFont boldSystemFontOfSize:17.0];
titleLabel.textColor = [UIColor whiteColor];
titleLabel.backgroundColor = [UIColor clearColor];
[self addSubview:titleLabel];
// Create and configure the disclosure button.
disclosureButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
disclosureButton.frame = CGRectMake(0.0, 5.0, 35.0, 35.0);
[disclosureButton setImage:[UIImage imageNamed:@"carat.png"] forState:UIControlStateNormal];
[disclosureButton setImage:[UIImage imageNamed:@"carat-open.png"] forState:UIControlStateSelected];
[disclosureButton addTarget:self action:@selector(toggleOpen:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:disclosureButton];
// Set the colors for the gradient layer.
static NSMutableArray *colors = nil;
if (colors == nil) {
colors = [[NSMutableArray alloc] initWithCapacity:3];
UIColor *color = nil;
color = [UIColor colorWithRed:0.82 green:0.84 blue:0.87 alpha:1.0];
[colors addObject:(id)[color CGColor]];
color = [UIColor colorWithRed:0.41 green:0.41 blue:0.59 alpha:1.0];
[colors addObject:(id)[color CGColor]];
color = [UIColor colorWithRed:0.41 green:0.41 blue:0.59 alpha:1.0];
[colors addObject:(id)[color CGColor]];
}
[(CAGradientLayer *)self.layer setColors:colors];
[(CAGradientLayer *)self.layer setLocations:[NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0], [NSNumber numberWithFloat:0.48], [NSNumber numberWithFloat:1.0], nil]];
}
return self;
}
-(IBAction)toggleOpen:(id)sender {
[self toggleOpenWithUserAction:YES];
}
-(void)toggleOpenWithUserAction:(BOOL)userAction {
// Toggle the disclosure button state.
disclosureButton.selected = !disclosureButton.selected;
// If this was a user action, send the delegate the appropriate message.
if (userAction) {
if (disclosureButton.selected) {
if ([delegate respondsToSelector:@selector(sectionHeaderView:sectionOpened:)]) {
[delegate sectionHeaderView:self sectionOpened:section];
}
}
else {
if ([delegate respondsToSelector:@selector(sectionHeaderView:sectionClosed:)]) {
[delegate sectionHeaderView:self sectionClosed:section];
}
}
}
}
- (void)dealloc {
[titleLabel release];
[disclosureButton release];
[super dealloc];
}
@end
1 个解决方案
#1
1
If you want to not activate the gesture recognizer for certain controls you should implement the (BOOL)gestureRecognizer: shouldReceiveTouch: delegate method of UIGestureRecognizer, for example the following will ignore the event from the recognizer for any button that is pressed
如果您不想为某些控件激活手势识别器,您应该实现(BOOL)gestureRecognizer:shouldReceiveTouch:UIGestureRecognizer的委托方法,例如,以下将忽略来自识别器的任何按下按钮的事件
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
return !( [touch.view isKindOfClass:[UIButton class]] );
}
hope it helps
希望能帮助到你
#1
1
If you want to not activate the gesture recognizer for certain controls you should implement the (BOOL)gestureRecognizer: shouldReceiveTouch: delegate method of UIGestureRecognizer, for example the following will ignore the event from the recognizer for any button that is pressed
如果您不想为某些控件激活手势识别器,您应该实现(BOOL)gestureRecognizer:shouldReceiveTouch:UIGestureRecognizer的委托方法,例如,以下将忽略来自识别器的任何按下按钮的事件
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
return !( [touch.view isKindOfClass:[UIButton class]] );
}
hope it helps
希望能帮助到你