移动时无法触摸按钮

时间:2011-06-09 07:23:58

标签: ipad uibutton nstimer

我写了下面的代码。我的问题是我无法触摸某些按钮。我在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]);
}

1 个答案:

答案 0 :(得分:1)

try set target:self action:buttonpressed

我认为你忘了这样做,当你点击它时会触发该功能

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