bme*_*nde 5 iphone uibutton ios
想象一下你的键盘.想象一下,自己将一根手指放在一个键上,然后(按住)将手指一直移动到键盘上的另一个键.现在,想象一下键盘上的每个键都是UIButton.当您将手指放在当前键上时,此键(UIButton)会突出显示.然后,当用户移动到另一个键时,不再突出显示第一个键,并且突出显示按下的当前键.
现在,我有一个6 x 8网格的UIButtons,每个53 x 53像素.所以,我有48个UIButtons.我想复制这种想法.用户手指所在的按钮将具有稍微更轻的图像(看起来像选择的那样),并且所有其他图像将不会稍微更轻.
这是我对如何解决这个问题的看法,
1)创建全部48 UIButtons英寸viewDidLoad.UIControlStateHighlighted为所有人添加较亮的图像UIButtons.
2)添加某种方式,target以touchUpOutside某种方式使当前按钮不突出显示,不可用.(可能设置突出显示,然后设置userInteractionEnabled为否).但是,我怎么能告诉下一个按钮被使用?我怎么能说我希望UIButton用户手指所具有的特定内容突出显示并用于检测手势和内容.
此外,这种touchUpOutside方法可能无法正常工作,因为所有按钮都是彼此相邻的,我认为你必须拖远才能运行它touchUpOutside.
同样重要的是要注意按钮没有放在相同的行和列中.有时一个按钮实际上是一个UIImage但看起来像一个按钮UIButton.因此,出于某种原因进行某种快速枚举比较帧是行不通的.
两个观察结果:
我通常使用手势识别器来处理类似的事情,而不是各种touchUp...方法。
我知道您说过您不希望通过子视图进行快速枚举,但为什么不呢?仅仅因为某些类与其他类不同并不是问题(因为您可以只测试该isKindOfClass方法)。(顺便说一句,我不确定为什么有些是按钮,有些不是。)仅仅因为它们没有排列起来也没有什么区别。
不管怎样,对于这样的东西,我经常将手势识别器放在共享父视图上(例如通常是视图控制器的视图),然后枚举子视图以查看当前 CGPoint 包含在哪个视图中?
CGPoint location = [sender locationInView:self.view];
for (UIView *subview in self.view.subviews)
{
if (CGRectContainsPoint(subview.frame, location))
{
// do your appropriate highlighting
if ([subview isKindOfClass:[UIButton class]])
{
// something for buttons
}
else if ([subview isKindOfClass:[UIImageView class]])
{
// something for imageviews
}
return;
}
}
Run Code Online (Sandbox Code Playgroud)
我不知道这对你来说是否有意义,但对我来说似乎最简单。如果你澄清你的意图,也许我可以进一步完善我的答案。
作为一个实际的例子,您可以定义一个连续的手势识别器,跟踪单击的第一个和最后一个子视图(如果您不在子视图上启动手势,则不会生成手势,如果您不这样做)不要在子视图上停止你的手势,它会取消整个事情),例如:
@interface SubviewGestureRecognizer : UIGestureRecognizer
@property (nonatomic,strong) UIView *firstSubview;
@property (nonatomic,strong) UIView *currentSubview;
@end
@implementation SubviewGestureRecognizer
- (id) initWithTarget:(id)target action:(SEL)action
{
self = [super initWithTarget:target action:action];
if (self)
{
self.firstSubview = nil;
self.currentSubview = nil;
}
return self;
}
// you might want to tweak this `identifySubview` to only look
// for buttons and imageviews, or items with nonzero tag properties,
// or recursively navigate if it encounters container UIViews
// or whatever suits your app
- (UIView *)identifySubview:(NSSet *)touches
{
CGPoint location = [[touches anyObject] locationInView:self.view];
for (UIView *subview in self.view.subviews)
{
if (CGRectContainsPoint(subview.frame, location))
{
return subview;
}
}
return nil;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
if ([touches count] != 1)
{
self.state = UIGestureRecognizerStateFailed;
return;
}
self.firstSubview = [self identifySubview:touches];
self.currentSubview = self.firstSubview;
if (self.firstSubview == nil)
self.state = UIGestureRecognizerStateFailed;
else
self.state = UIGestureRecognizerStateBegan;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesMoved:touches withEvent:event];
if (self.state == UIGestureRecognizerStateFailed) return;
self.currentSubview = [self identifySubview:touches];
self.state = UIGestureRecognizerStateChanged;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesEnded:touches withEvent:event];
self.currentSubview = [self identifySubview:touches];
if (self.currentSubview != nil)
self.state = UIGestureRecognizerStateEnded;
else
self.state = UIGestureRecognizerStateFailed;
}
- (void)reset
{
[super reset];
self.firstSubview = nil;
self.currentSubview = nil;
}
Run Code Online (Sandbox Code Playgroud)
然后您可以在以下位置进行设置viewDidLoad:
SubviewGestureRecognizer *recognizer = [[SubviewGestureRecognizer alloc] initWithTarget:self action:@selector(handleTouches:)];
[self.view addGestureRecognizer:recognizer];
Run Code Online (Sandbox Code Playgroud)
然后定义您的处理程序(这适用于按钮和图像视图,利用两者都支持的事实setHighlighted):
- (void)handleTouches:(SubviewGestureRecognizer *)sender
{
static UIControl *previousControl = nil;
if (sender.state == UIGestureRecognizerStateBegan || sender.state == UIGestureRecognizerStateChanged)
{
if (sender.state == UIGestureRecognizerStateBegan)
previousControl = nil;
UIView *subview = sender.currentSubview;
if (previousControl != subview)
{
// reset the old one (if any)
[previousControl setHighlighted:NO];
// highlight the new one
previousControl = (UIControl *)subview;
[previousControl setHighlighted:YES];
}
}
else if (sender.state == UIGestureRecognizerStateEnded)
{
if (previousControl)
{
[previousControl setHighlighted:NO];
NSLog(@"successfully touchdown on %@ and touchup on %@", sender.firstSubview, sender.currentSubview);
}
}
else if (sender.state == UIGestureRecognizerStateCancelled || sender.state == UIGestureRecognizerStateFailed)
{
[previousControl setHighlighted:NO];
NSLog(@"cancelled/failed gesture");
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
266 次 |
| 最近记录: |