移动时无法触摸按钮

发布于 2024-11-14 11:57:23 字数 3273 浏览 3 评论 0原文

我编写了如下所示的代码。我的问题是我无法触摸某些按钮。我已经在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北恋 2024-11-21 11:57:23

尝试设置 target:self action:buttonpressed

我想你忘记这样做了,当你点击它时该函数将被触发

[button addTarget:self action:@selector(buttonpressed:) forControlEvents:UIControlEventTouchUpInside];

try set target:self action:buttonpressed

I think you forget to do that , the function will be fired when you click it

[button addTarget:self action:@selector(buttonpressed:) forControlEvents:UIControlEventTouchUpInside];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文