开发者

how to move the UIButton

i create a UIButton *actionbtn,it's default image is 1.png,and hightlight image is 2.png, i select this button,and move it to any place of the开发者_开发问答 screen.my code is

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint touchMoved = [touch locationInView:self];
    actionbtn.frame = CGRectMake(touchMoved.x-40, touchMoved.y-40, 80, 80);
}

if i clicked the button and move it,it can not be moved,but if i touch the screen,and move on the screen the button can work...


Try this:

http://developer.apple.com/library/ios/#samplecode/MoveMe/Introduction/Intro.html#//apple_ref/doc/uid/DTS40007315

It's an example with an UIButton that move on screen...


The button is moving when you keep touching the screen because you implemented -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;. If you attempted to move the button when simply touching the screen anywhere, place that code in -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;.


add an if clause in touchesMoved function.

UITouch *touch = [touches anyObject];    
if( touch.view == button){
     [button.center = [touch locationInView:self.view];
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜