嗨即時嘗試添加一個按鈕,在用戶點擊屏幕上的點。在UIView點擊點添加按鈕
這裏是我的UIView文件
- (void)drawRect:(CGRect)rect {
// Drawing code
NSLog(@"drawRect, %i, %i", firstTouch.x, firstTouch.y);
[tagButton drawRect:CGRectMake(firstTouch.x, firstTouch.y, 100, 200)];
[self addSubview:tagButton];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
firstTouch = [touch locationInView:self];
[self setNeedsDisplay];
}
的代碼,這是從日誌firstTouch
2009-10-01 17:27:23.743文[2521:207]的drawRect,0,1080311808
我該如何獲得觸摸的x,y點,並在該點上創建一個uibutton?
任何幫助,將不勝感激,謝謝
這就是我想出,其添加的按鈕視圖。但是當我彈出這個視圖時,它會崩潰。我似乎無法找到任何問題。任何人都可以看到問題是什麼?
UIButton *newButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[newButton setFrame:CGRectMake(self.firstTouch.x, self.firstTouch.y, width, 17)];
[newButton setTitle:[textField text] forState:UIControlStateNormal];
[newButton setFont:[UIFont boldSystemFontOfSize:12]];
[newButton setShowsTouchWhenHighlighted:YES];
[newButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[newButton setTitleShadowColor:[UIColor blackColor] forState:UIControlStateNormal];
[newButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
[newButton setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
[newButton setBackgroundColor:[UIColor clearColor]];
UIImage *bgImg = [[UIImage imageNamed:@"button_greyblue.png"] stretchableImageWithLeftCapWidth:15.0 topCapHeight:0.0];
[newButton setBackgroundImage:bgImg forState:UIControlStateNormal];
[self setTagButton:newButton];
[self.view addSubview:tagButton];
[newButton release];
我遇到了一個新問題,你可以請看看嗎? – vicky 2009-10-02 07:22:57