移动时无法触摸按钮
我编写了如下所示的代码。我的问题是我无法触摸某些按钮。我已经在 NSTimer 方法中编写了该方法。有人可以帮助解决这个问题吗?
一种方法是创建一个按钮,另一种方法是根据屏幕尺寸更改值。
-(void)createButton
{
int x=0;
int y=0;
int width=100;
int height=100;
for (int i=0; i<9; i++)
{
UIButton *button=[[UIButton alloc] initWithFrame:CGRectMake(x ,y, width, height)];
[button.titleLabel setFont:[UIFont fontWithName:@"Helvetica" size:24.0]];
NSDate *date=[NSDate date];
NSDateFormatter *dateformatter=[[NSDateFormatter alloc]init];
[dateformatter setDateFormat:@"ss"];
NSString *sec=[dateformatter stringFromDate:date];
NSLog(@"sec is %@",sec);
CGPoint dest= CGPointMake(arc4random() %768, arc4random() %1024);
//CGPoint dest= CGPointMake(arc4random() %[sec intValue], arc4random() %480);
button.tag=i;
[button setBackgroundColor:[UIColor blackColor]];
[button setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
[button setTitle:[NSString stringWithFormat:@"%d",i] forState:UIControlStateNormal];
//[button setImage:[UIImage imageNamed:@"thing.png"] forState:UIControlStateNormal];
int numx=[[NSNumber numberWithInt:dest.x]intValue];
int numy=[[NSNumber numberWithInt:dest.y]intValue];
//button.center.x= numx+5;
//button.center.y=numy+5;
button.center=CGPointMake(numx+10, numy+10);
NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
[dict setValue:[NSNumber numberWithInt:5] forKey:@"Xvalue"];
[dict setValue:[NSNumber numberWithInt:5] forKey:@"Yvalue"];
[dict setValue:button forKey:@"Button"];
[button addTarget:self action:@selector(buttonpressed:) forControlEvents:UIControlEventTouchUpInside];
//[button addTarget:self action:@selector(dragBegan:withEvent:)forControlEvents: UIControlEventTouchDown];
//[button addTarget:@selector(buttonpressed:) action:@selector(buttonpressed:) forControlEvents:UIControlStateSelected];
[button addTarget:self action:@selector(dragMoving:withEvent:)forControlEvents: UIControlEventTouchDragInside];
[button addTarget:self action:@selector(dragEnded:withEvent:)forControlEvents: UIControlEventTouchUpInside |
UIControlEventTouchUpOutside];
[self.buttonArray addObject:dict];
[self.view addSubview:button];
[button release];
x+=50;
}
}
-(void)timerMethod
{
for(NSMutableDictionary *dict in self.buttonArray)
{
UIButton *but=[dict objectForKey:@"Button"];
CGPoint bcenter=but.center;
int numx=[[dict objectForKey:@"Xvalue"]intValue];
int numy=[[dict objectForKey:@"Yvalue"]intValue];
bcenter.x+=numx;
bcenter.y+=numy;
//bcenter.x=bcenter.x+[dict objectForKey:@"Xvalue"];
//bcenter.y=bcenter.y+[dict objectForKey:@"Yvalue"];
but.center=bcenter;
if(bcenter.x>720)
{
[dict setValue:[NSNumber numberWithInt:-10] forKey:@"Xvalue"];
}
if(bcenter.y>1000)
{
[dict setValue:[NSNumber numberWithInt:-10] forKey:@"Yvalue"];
}
if(bcenter.x<15)
{
[dict setValue:[NSNumber numberWithInt:10] forKey:@"Xvalue"];
}
if(bcenter.y<15)
{
[dict setValue:[NSNumber numberWithInt:10] forKey:@"Yvalue"];
}
}
}
-(void)buttonpressed:(id)sender
{
NSLog(@"sender tag is %d",[sender tag]);
}
I have written the code shown below. my problem is that I can't touch some of the buttons. I have written that method in NSTimer method. Can anybody help in solving this problem?
One method is to create a button and another for changing the values to screen size.
-(void)createButton
{
int x=0;
int y=0;
int width=100;
int height=100;
for (int i=0; i<9; i++)
{
UIButton *button=[[UIButton alloc] initWithFrame:CGRectMake(x ,y, width, height)];
[button.titleLabel setFont:[UIFont fontWithName:@"Helvetica" size:24.0]];
NSDate *date=[NSDate date];
NSDateFormatter *dateformatter=[[NSDateFormatter alloc]init];
[dateformatter setDateFormat:@"ss"];
NSString *sec=[dateformatter stringFromDate:date];
NSLog(@"sec is %@",sec);
CGPoint dest= CGPointMake(arc4random() %768, arc4random() %1024);
//CGPoint dest= CGPointMake(arc4random() %[sec intValue], arc4random() %480);
button.tag=i;
[button setBackgroundColor:[UIColor blackColor]];
[button setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
[button setTitle:[NSString stringWithFormat:@"%d",i] forState:UIControlStateNormal];
//[button setImage:[UIImage imageNamed:@"thing.png"] forState:UIControlStateNormal];
int numx=[[NSNumber numberWithInt:dest.x]intValue];
int numy=[[NSNumber numberWithInt:dest.y]intValue];
//button.center.x= numx+5;
//button.center.y=numy+5;
button.center=CGPointMake(numx+10, numy+10);
NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
[dict setValue:[NSNumber numberWithInt:5] forKey:@"Xvalue"];
[dict setValue:[NSNumber numberWithInt:5] forKey:@"Yvalue"];
[dict setValue:button forKey:@"Button"];
[button addTarget:self action:@selector(buttonpressed:) forControlEvents:UIControlEventTouchUpInside];
//[button addTarget:self action:@selector(dragBegan:withEvent:)forControlEvents: UIControlEventTouchDown];
//[button addTarget:@selector(buttonpressed:) action:@selector(buttonpressed:) forControlEvents:UIControlStateSelected];
[button addTarget:self action:@selector(dragMoving:withEvent:)forControlEvents: UIControlEventTouchDragInside];
[button addTarget:self action:@selector(dragEnded:withEvent:)forControlEvents: UIControlEventTouchUpInside |
UIControlEventTouchUpOutside];
[self.buttonArray addObject:dict];
[self.view addSubview:button];
[button release];
x+=50;
}
}
-(void)timerMethod
{
for(NSMutableDictionary *dict in self.buttonArray)
{
UIButton *but=[dict objectForKey:@"Button"];
CGPoint bcenter=but.center;
int numx=[[dict objectForKey:@"Xvalue"]intValue];
int numy=[[dict objectForKey:@"Yvalue"]intValue];
bcenter.x+=numx;
bcenter.y+=numy;
//bcenter.x=bcenter.x+[dict objectForKey:@"Xvalue"];
//bcenter.y=bcenter.y+[dict objectForKey:@"Yvalue"];
but.center=bcenter;
if(bcenter.x>720)
{
[dict setValue:[NSNumber numberWithInt:-10] forKey:@"Xvalue"];
}
if(bcenter.y>1000)
{
[dict setValue:[NSNumber numberWithInt:-10] forKey:@"Yvalue"];
}
if(bcenter.x<15)
{
[dict setValue:[NSNumber numberWithInt:10] forKey:@"Xvalue"];
}
if(bcenter.y<15)
{
[dict setValue:[NSNumber numberWithInt:10] forKey:@"Yvalue"];
}
}
}
-(void)buttonpressed:(id)sender
{
NSLog(@"sender tag is %d",[sender tag]);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试设置 target:self action:buttonpressed
我想你忘记这样做了,当你点击它时该函数将被触发
try set target:self action:buttonpressed
I think you forget to do that , the function will be fired when you click it