使用UIView或覆盖UIButton制作自定义按钮? [英] Making a custom Button using a UIView or overriding UIButton?

查看:99
本文介绍了使用UIView或覆盖UIButton制作自定义按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过代码生成自定义按钮,这就是我目前正在做的事情。

I need to generate a custom button through code, this is how i am currently doing it.

-(void) initialiseButtons 
{
    int ypos = playerImage.frame.origin.y + playerImage.frame.size.height + 8;
    for(int i=0; i<totalButtons; i++) 
    {       
        UIButton *newButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];    
        [newButton setFrame:CGRectMake(20, ypos, 220, 30)];
        newButton.tag = 10 + i;
        [newButton addTarget:self action:@selector(statTapped:) forControlEvents:UIControlEventTouchUpInside];
        [self.frontView addSubview:newButton];
        ypos += 30 + 7;
    }
}

通过代码完美地创建空白按钮,给出它们一个标签,并在touchUpInside上分配一个回调函数。

This creates the blank buttons perfectly through code, gives them a tag and assigns an callback function on touchUpInside.

自定义按钮需要能够处理按下时显示图像。
它需要能够绘制2个文本。 1与按钮的左侧对齐,1与按钮的右侧对齐。

The custom button needs to be able to handle showing an image when pressed down. It needs to be able to draw 2 pieces of Text. 1 aligned to the left hand side of the button and 1 aligned to the righthand side of the button.

我的老板建议使用View而不是按钮。我不明白这是如何工作的。当我开始考虑它时,我认为它需要一个专用于按钮的viewcontroller。还有一些绘制方法?这听起来很复杂,我不知道如何做到这一点。

My boss suggested instead of buttons I use a View. I dont understand how this will work. When i start thinking about it, i think it would require having a viewcontroller dedicated to the buttons. And some draw method? It sounds complicated and I am not grasping how it can be done.

通过使自定义类重写UIButton,是否有更简单的方法?我之前做了一个简单的测试类,但当我使用它们代替Normal UIButton类时,按钮位置没有绘制任何内容。我预计我会看到buttonUp.png被绘制。

Is there a simpler method by making a custom class overriding UIButton? I made a simple test class earlier but nothing was drawn in the buttons locations when I used them in place of the Normal UIButton class. I expected i would see the buttonUp.png drawn.

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface CardButton : UIButton {
}
@end


#import "CardButton.h"
#import <UIKit/UIKit.h>
@implementation CardButton
- (id)initWithFrame:(CGRect)frame 
{
    if (self = [super initWithFrame:frame]) 
    {
        [self setImage:[UIImage imageNamed:@"buttonUp.png"] forStates:UIControlStateNormal];
        self.adjustsImageWhenHighlighted = YES;
    }
    return self;
}
@end

有人能给我一些指示吗?我此刻很沮丧。一直在读一些与按钮相关的其他各种线程,但没有任何内容让我明白如何解决这个问题

Anybody able to give me some pointers on this? I'm pretty blocked at the moment. Have been reading some various other threads related to buttons but nothing that made it clear in my head how to tackle the problem

推荐答案

你不应该(我几乎可以说你不能)继承 UIButton ,因为它原来是一个类集群,它是不切实际的(读:不可能)到从中得出。

You should not (I'd almost say you can't) subclass UIButton, as it turns out to be a class cluster, and it would be impractical (read: impossible) to derive from that.

所以你的两个选择是:

1)使用标准自定义类型的UIButton ,添加您想要显示的元素(即<$​​ c $ c> UILabel )并连接触摸和触摸的动作并做出相应的反应(更改视图,触发操作等。)

1) Use standard UIButton of custom type, add the elements you want to show (i.e. UILabel) and hook up actions for touch down and touch up and react accordingly (change the views, trigger actions etc.)

2)使用自定义 UIView ,实现 drawRect:绘制您喜欢的方式或使用自定义视图作为子视图,如1)。然后使用 touchesBegan: touchesEnded:等消息作出相应的反应(或 UIGestureRecognizer 如果你可以忍受3.2+兼容性。)

2) Use a custom UIView, implement drawRect: to draw how you like it or use custom views as subviews as in 1). Then use the touchesBegan:, touchesEnded: etc. messages to react accordingly (or UIGestureRecognizer if you can live with 3.2+ compatibility).

你可以建立一个工厂来构建这些按钮,如果它们都非常相似,那么你的代码变得干净且无重复。

You might build a factory to build those buttons if they all are very similar, so that your code becomes clean and non-repetive.

根据iOS 6编辑:
Apple现在提到UIButton文档中的子类化,所以虽然定义不太明确,但它现在似乎很安全。

Edit as per iOS 6: Apple now mentions subclassing in the UIButton docs, so while still not very well defined, it seems quite safe now to do so.

这篇关于使用UIView或覆盖UIButton制作自定义按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆