2023年7月13日发(作者:)
IOSUIButton⽤法这段代码动态的创建了⼀个UIButton,并且把相关常⽤的属性都列举了.希望对⼤家有⽤.//这⾥创建⼀个圆⾓矩形的按钮UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];// 能够定义的button类型有以下6种,// typedef enum {// UIButtonTypeCustom = 0, ⾃定义风格// UIButtonTypeRoundedRect, 圆⾓矩形// UIButtonTypeDetailDisclosure, 蓝⾊⼩箭头按钮,主要做详细说明⽤// UIButtonTypeInfoLight, 亮⾊感叹号// UIButtonTypeInfoDark, 暗⾊感叹号// UIButtonTypeContactAdd, ⼗字加号按钮// } UIButtonType;//给定button在view上的位置 = CGRectMake(20, 20, 280, 40);//button背景⾊oundColor = [UIColor clearColor];//设置button填充图⽚//[button1 setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];//设置button标题[button1 setTitle:@"点击" forState:UIControlStateNormal];/* forState: 这个参数的作⽤是定义按钮的⽂字或图⽚在何种状态下才会显现*///以下是⼏种状态// enum {// UIControlStateNormal = 0, 常规状态显现// UIControlStateHighlighted = 1 << 0, ⾼亮状态显现// UIControlStateDisabled = 1 << 1, 禁⽤的状态才会显现// UIControlStateSelected = 1 << 2, 选中状态// UIControlStateApplication = 0x00FF0000, 当应⽤程序标志时// UIControlStateReserved = 0xFF000000 为内部框架预留,可以不管他// };/*默认情况下,当按钮⾼亮的情况下,图像的颜⾊会被画深⼀点,如果这下⾯的这个属性设置为no,那么可以去掉这个功能/sImageWhenHighlighted = NO;/跟上⾯的情况⼀样,默认情况下,当按钮禁⽤的时候,图像会被画得深⼀点,设置NO可以取消设置/sImageWhenDisabled = NO;/ 下⾯的这个属性设置为yes的状态下,按钮按下会发光*/ouchWhenHighlighted = YES;/* 给button添加事件,事件有很多种,我会单独开⼀篇博⽂介绍它们,下⾯这个时间的意思是按下按钮,并且⼿指离开屏幕的时候触发这个事件,跟web中的click事件⼀样。触发了这个事件以后,执⾏butClick:这个⽅法,addTarget:self 的意思是说,这个⽅法在本类中也可以传⼊其他类的指针*/[button1 addTarget:self action:@selector(butClick:) forControlEvents:UIControlEventTouchUpInside];//显⽰控件[ addSubview:button1];注意:[button1 addTarget:selfaction:@selector(alarmTimeDone:)forControlEvents:UIControlEventTouchUpInside];addTarget:self 是链接到self,⼀般都这样设置action:@selector(alarmTimeDone:) 时间处理函数forControlEvents:UIControlEventTouchUpInside 控件事件处理的消息//取消按钮已经添加的所有事件:(这个⽐较重要,若添加了两个事件 两个事件都会被触发)[btn removeTarget:nil action:nil forControlEvents:UIControlEventTouchUpInside];何时释放release UIButton?是否在dealloc中对UIButton对象进⾏release操作,取决于UIButton初始化的⽅式。如果使⽤[UIButtonbuttonWithType:UIButtonTypeRoundedRect]这种⽅式,是不需要进⾏release操作的,因为这种⽅式是⾃动释放的。如果使⽤ [[UIButton alloc]init]的⽅式,则需要主动进⾏release释放操作。IOS UIButton事件:UIControlEventTouchDown单点触摸按下事件:⽤户点触屏幕,或者⼜有新⼿指落下的时候。UIControlEventTouchDownRepeat多点触摸按下事件,点触计数⼤于1:⽤户按下第⼆、三、或第四根⼿指的时候。UIControlEventTouchDragInside当⼀次触摸在控件窗⼝内拖动时。UIControlEventTouchDragOutside当⼀次触摸在控件窗⼝之外拖动时。UIControlEventTouchDragEnter当⼀次触摸从控件窗⼝之外拖动到内部时。UIControlEventTouchDragExit当⼀次触摸从控件窗⼝内部拖动到外部时。UIControlEventTouchUpInside所有在控件之内触摸抬起事件。UIControlEventTouchUpOutside所有在控件之外触摸抬起事件(点触必须开始与控件内部才会发送通知)。UIControlEventTouchCancel所有触摸取消事件,即⼀次触摸因为放上了太多⼿指⽽被取消,或者被上锁或者电话呼叫打断。UIControlEventTouchChanged当控件的值发⽣改变时,发送通知。⽤于滑块、分段控件、以及其他取值的控件。你可以配置滑块控件何时发送通知,在滑块被放下时发送,或者在被拖动时发送。UIControlEventEditingDidBegin当⽂本控件中开始编辑时发送通知。UIControlEventEditingChanged当⽂本控件中的⽂本被改变时发送通知。UIControlEventEditingDidEnd当⽂本控件中编辑结束时发送通知。UIControlEventEditingDidOnExit当⽂本控件内通过按下回车键(或等价⾏为)结束编辑时,发送通知。UIControlEventAlltouchEvents通知所有触摸事件。UIControlEventAllEditingEvents通知所有关于⽂本编辑的事件。UIControlEventAllEvents通知所有事件。实例: UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
// = rect_screen;
= CGRectMake( - 20.0f - 30.0f, - 50.0f, 30.0f, 50.0f);
oundColor = [UIColor blueColor];
// UIControlEventTouchDragInside
// UIControlEventTouchDragOutside
[btn addTarget:self action:@selector(dragInside) forControlEvents:UIControlEventTouchDragInside];
[btn addTarget:self action:@selector(dragOutside) forControlEvents:UIControlEventTouchDragOutside];
// dismissView
[btn addTarget:self action:@selector(upInside) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:btn];
return self;
}
- (void)dragInside
{
NSLog(@"");
}
- (void)dragOutside
{
NSLog(@"");
}
- (void)upInside
{
NSLog(@"");
}
长按事件UIButton *aBtn=[UIButton buttonWithType:UIButtonTypeCustom];
[aBtn setFrame:CGRectMake(40, 100, 60, 60)];
[aBtn setBackgroundImage:[UIImage imageNamed:@""]forState:UIControlStateNormal];
//button点击事件
[aBtn addTarget:self action:@selector(btnShort:)forControlEvents:UIControlEventTouchUpInside];
//button长按事件
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:selfaction:@selector(btnLong:)];
mPressDuration = 0.8; //定义按的时间
[aBtn addGestureRecognizer:longPress];
-(void)btnLong:(UILongPressGestureRecognizer*)gestureRecognizer{
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
NSLog(@"长按事件");
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"消息" message:@"确定删除该模式吗?" delegate:selfcancelButtonTitle:@"取消" otherButtonTitles:@"删除", nil nil];
[alert show];
}
}
发布者:admin,转转请注明出处:http://www.yc00.com/xiaochengxu/1689262572a226328.html
评论列表(0条)