Sportica   Sportica

iOS 按钮拖动。

 

-(void)testMove

{

    moveBtn = [[UIButton alloc ]init];

    moveBtn.frame = CGRectMake(0, 30, 60, 60);

    moveBtn.backgroundColor = [UIColorredColor];

    //[moveBtn addTarget:self action:@selector(moveBtn) forControlEvents:UIControlEventTouchDown];

    //[moveBtn addTarget:self action:@selector(moveBtnEnd) forControlEvents:UIControlEventTouchUpInside];

    

    [moveBtnaddTarget:selfaction:@selector(dragMoving:withEvent: )forControlEvents: UIControlEventTouchDragInside];

    [moveBtnaddTarget:selfaction:@selector(doClick:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:moveBtn];

}

 

- (void) dragMoving: (UIButton *)btn withEvent:(UIEvent *)event

{

    CGPoint point = [[[event allTouches] anyObject] locationInView:self.view];

    CGFloat x = point.x;

    CGFloat y = point.y;

    

    CGFloat btnx = btn.frame.size.width/2;

    CGFloat btny = btn.frame.size.height/2;

    

    if(x<=btnx)

    {

        point.x = btnx;

    }

    if(x >= self.view.bounds.size.width - btnx)

    {

        point.x = self.view.bounds.size.width - btnx;

    }

    

    NSLog(@"fs:%f %f",x, btnx);

    btn.center = point;

    NSLog(@"%f,,,%f",btn.center.x,btn.center.y);

}

posted @ 2014-03-26 18:38  qingjoin  阅读(2987)  评论(0编辑  收藏  举报
  Sportica