开发者社区 问答 正文

iphone,实现拖拽到同一位置

应用中有拖拽选项,需要实现将对象拖拽到位置之后,在同一位置设置对象,但是我下面的代码实现不了,不知道怎么办?是在IOS6版本中的,

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint touchPoint = [[touches anyObject] locationInView:self.view];
if (touchPoint.x > self.img1.frame.origin.x &&
    touchPoint.x < self.img1.frame.origin.x + self.img1.frame.size.width &&
    touchPoint.y > self.img1.frame.origin.y &&
    touchPoint.y < self.img1.frame.origin.y + self.img1.frame.size.height )
{
    self.img1.backgroundColor = self.img1.backgroundColor;

}
self.img1.frame = CGRectMake(self.homePosition.x, self.homePosition.y,
                                   self.img1.frame.size.width,
                                   self.img1.frame.size.height);
}

展开
收起
爵霸 2016-03-26 09:16:44 1869 分享 版权
1 条回答
写回答
取消 提交回答
  • 在.h文件:

    CGPoint startPt;

    在.m文件:

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
        startPt = [[touches anyObject] locationInView:self.view];
    
    }
    
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    {
    CGPoint touchPoint = [[touches anyObject] locationInView:self.view];
    if (touchPoint.x > self.img1.frame.origin.x &&
        touchPoint.x < self.img1.frame.origin.x + self.img1.frame.size.width &&
        touchPoint.y > self.img1.frame.origin.y &&
        touchPoint.y < self.img1.frame.origin.y + self.img1.frame.size.height )
    {
        self.img1.backgroundColor = self.img1.backgroundColor;
    
    }
    
    self.img1.frame = CGRectMake(startPt.x, startPt.y,
                                       self.img1.frame.size.width,
                                       self.img1.frame.size.height);
    }
    2019-07-17 19:15:14
    赞同 展开评论
问答分类:
问答标签:
问答地址: