如何读取按钮的坐标?

时间:2012-03-23 09:58:58

标签: objective-c

UIView *contentView = [[UIView alloc] initWithFrame: [[UIScreen mainScreen] applicationFrame]];
contentView.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"background.jpg"]];
self.view = contentView;
[contentView release];
[contentView addSubview:scrollView];

scrollView.contentSize = CGSizeMake(1800,480);
UIScrollView* tscrollView = [[UIScrollView alloc] initWithFrame:(CGRect){.origin.x = 0.0f, .origin.y = 0.0f, .size.width = 320.0f, .size.height = 5480.0f}];
tscrollView.contentSize = CGSizeMake(1800,192);

Object1 = [UIButton buttonWithType:1];  
Object1.frame = CGRectMake(383, 250, 256, 192);  
UIImage *buttonImage = [UIImage imageNamed:@"02_1024_768.jpg"];
[Object1 setBackgroundImage:buttonImage forState:UIControlStateNormal];
//[self.view addSubview:Object1];
[scrollView addSubview:Object1];

Object5 = [UIButton buttonWithType:1];
Object5.frame = CGRectMake(1487, 250, 256, 192);
UIImage *buttonImage1 = [UIImage imageNamed:@"p5.jpg"];
[Object5 setBackgroundImage:buttonImage1 forState:UIControlStateNormal];
[scrollView addSubview:Object5];

UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(400, 100, 200, 200)];
[myLabel setBackgroundColor:[UIColor clearColor]];
myLabel.text = @"Button1 is in range";
[myLabel setTextAlignment:UITextAlignmentCenter];
[myLabel setTextColor:[UIColor whiteColor]];

NSLog(@"x = %d",Object1.center.x);
position = CGPointMake(0,0);
Object1.center = CGPointMake(Object1.center.x+position.x,Object1.center.y+position.y);

if((Object1.center.x >341) && (Object1.center.x < 597)){
    [myLabel setHidden:NO];
}
else {
    [myLabel setHidden:YES];
}
[self.view addSubview:myLabel];

这是代码,但似乎x坐标总是等于0.可能some1告诉我为什么以及如何修复它?提前谢谢。

1 个答案:

答案 0 :(得分:0)

试试这个

NSLog(@"x=%f",Object1.center.x);

坐标是浮点值,你打印得像int,所以你得到0.我检查项目中的代码