在屏幕上移动UIView,为什么这样做?

时间:2022-10-15 20:03:53

First of all, this code works fine, it does exactly what i need it to, however after reviewing it, i am missing something.

首先,这段代码工作正常,它完全符合我的需要,但是在审阅之后,我错过了一些东西。

We have a card. When touched the card moves. When in some location it goes to that location.

我们有一张卡片。触摸时卡片会移动。在某个地方,它会去那个地方。

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    ...
    //  Take note of the starting position and current position, both the same
    startTouchPoint = pt;
    currentTouchPoint = pt;
    ...    
}

-(void) touchesMoved:(NSSet *) touches withEvent:(UIEvent *)event {
    ...
    // As touch moves around the screen, current touch point is 
    // updated with it's new location
    currentTouchPoint = [first locationInView:self];
    ...
} 

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    ...
    // If something happens, put card in a given location
    cardCurrentlyBeingTouched.xPosition = 300;
    cardCurrentlyBeingTouched.yPosition = 150;
    ...
}

-(void) drawRect:(CGRect)rect {
   ...
   // Card moving around the screen
   cardCurrentlyBeingTouched.xPosition += (self.currentTouchPoint.x - startTouchPoint.x);
   cardCurrentlyBeingTouched.yPosition += (self.currentTouchPoint.y - startTouchPoint.y); 

   CGPoint moveToLoc = CGPointMake(cardCurrentlyBeingTouched.xPosition, cardCurrentlyBeingTouched.yPosition);

   [self addSubview:[cardCurrentlyBeingTouched uiImageViewAtPoint:moveToLoc rotated:0 withFaceImage:YES]];

   startTouchPoint.x = currentTouchPoint.x;
   startTouchPoint.y = currentTouchPoint.y;
   ...
}

So, in drawRect, (at all times) self.currentTouchPoint.x is the same as startTouchPoint.x (as Y), how in the world card's X and Y positions are changing if delta is always 0? (X and Y positions are CGPoints, when I printed them for comparison, i printed floats)

因此,在drawRect中,(始终)self.currentTouchPoint.x与startTouchPoint.x(作为Y)相同,如果delta始终为0,则世界卡的X和Y位置如何变化? (X和Y位置是CGPoints,当我打印它们进行比较时,我打印浮动)

Could it be a precision issue?

这可能是一个精确的问题吗?

This is my code, i wrote it, it works. But why? :)

这是我的代码,我写了它,它的工作原理。但为什么? :)

1 个解决方案

#1


1  

As seen from the snapshot below, there is no miracle after all. The lesson here is that drawRect is called awfully often (which is a good thing of course)

从下面的快照可以看出,毕竟没有奇迹。这里的教训是经常调用drawRect(当然这是一件好事)

> 2011-12-22 21:38:56.912 myApp[8923:f803] -- 266.000000 and 266.000000
> 2011-12-22 21:38:56.913 myApp[8923:f803] -- 266.000000 and 266.000000 
> 2011-12-22 21:38:56.915 myApp[8923:f803] -- 282.000000 and 266.000000    <---
> 2011-12-22 21:38:56.922 myApp[8923:f803] -- 282.000000 and 282.000000 
> 2011-12-22 21:38:56.923 myApp[8923:f803] -- 282.000000 and 282.000000 
> 2011-12-22 21:38:56.925 myApp[8923:f803] -- 282.000000 and 282.000000 
> 2011-12-22 21:38:56.932 myApp[8923:f803] -- 282.000000 and 282.000000 
> 2011-12-22 21:38:56.933 myApp[8923:f803] -- 282.000000 and 282.000000 
> 2011-12-22 21:38:56.934 myApp[8923:f803] -- 293.000000 and 282.000000    <---
> 2011-12-22 21:38:56.941 myApp[8923:f803] -- 293.000000 and 293.000000 
> 2011-12-22 21:38:56.942 myApp[8923:f803] -- 293.000000 and 293.000000 
> 2011-12-22 21:38:56.943 myApp[8923:f803] -- 293.000000 and 293.000000 
> 2011-12-22 21:38:56.950 myApp[8923:f803] -- 293.000000 and 293.000000 
> 2011-12-22 21:38:56.951 myApp[8923:f803] -- 293.000000 and 293.000000 
> 2011-12-22 21:38:56.952 myApp[8923:f803] -- 308.000000 and 293.000000    <---
> 2011-12-22 21:38:56.958 myApp[8923:f803] -- 308.000000 and 308.000000 
> 2011-12-22 21:38:56.959 myApp[8923:f803] -- 308.000000 and 308.000000 
> 2011-12-22 21:38:56.960 myApp[8923:f803] -- 320.000000 and 308.000000    <---
> 2011-12-22 21:38:56.975 myApp[8923:f803] -- 320.000000 and 320.000000 
> 2011-12-22 21:38:56.976 myApp[8923:f803] -- 320.000000 and 320.000000 
> 2011-12-22 21:38:56.977 myApp[8923:f803] -- 330.000000 and 320.000000 
> 2011-12-22 21:38:56.987 myApp[8923:f803] -- 330.000000 and 330.000000 
> 2011-12-22 21:38:56.988 myApp[8923:f803] -- 330.000000 and 330.000000 
> 2011-12-22 21:38:56.990 myApp[8923:f803] -- 339.000000 and 330.000000    <---
> 2011-12-22 21:38:56.999 myApp[8923:f803] -- 339.000000 and 339.000000 
> 2011-12-22 21:38:57.001 myApp[8923:f803] -- 339.000000 and 339.000000 
> 2011-12-22 21:38:57.002 myApp[8923:f803] -- 339.000000 and 339.000000 
> 2011-12-22 21:38:57.012 myApp[8923:f803] -- 339.000000 and 339.000000 
> 2011-12-22 21:38:57.013 myApp[8923:f803] -- 339.000000 and 339.000000 
> 2011-12-22 21:38:57.015 myApp[8923:f803] -- 349.000000 and 339.000000    <---

#1


1  

As seen from the snapshot below, there is no miracle after all. The lesson here is that drawRect is called awfully often (which is a good thing of course)

从下面的快照可以看出,毕竟没有奇迹。这里的教训是经常调用drawRect(当然这是一件好事)

> 2011-12-22 21:38:56.912 myApp[8923:f803] -- 266.000000 and 266.000000
> 2011-12-22 21:38:56.913 myApp[8923:f803] -- 266.000000 and 266.000000 
> 2011-12-22 21:38:56.915 myApp[8923:f803] -- 282.000000 and 266.000000    <---
> 2011-12-22 21:38:56.922 myApp[8923:f803] -- 282.000000 and 282.000000 
> 2011-12-22 21:38:56.923 myApp[8923:f803] -- 282.000000 and 282.000000 
> 2011-12-22 21:38:56.925 myApp[8923:f803] -- 282.000000 and 282.000000 
> 2011-12-22 21:38:56.932 myApp[8923:f803] -- 282.000000 and 282.000000 
> 2011-12-22 21:38:56.933 myApp[8923:f803] -- 282.000000 and 282.000000 
> 2011-12-22 21:38:56.934 myApp[8923:f803] -- 293.000000 and 282.000000    <---
> 2011-12-22 21:38:56.941 myApp[8923:f803] -- 293.000000 and 293.000000 
> 2011-12-22 21:38:56.942 myApp[8923:f803] -- 293.000000 and 293.000000 
> 2011-12-22 21:38:56.943 myApp[8923:f803] -- 293.000000 and 293.000000 
> 2011-12-22 21:38:56.950 myApp[8923:f803] -- 293.000000 and 293.000000 
> 2011-12-22 21:38:56.951 myApp[8923:f803] -- 293.000000 and 293.000000 
> 2011-12-22 21:38:56.952 myApp[8923:f803] -- 308.000000 and 293.000000    <---
> 2011-12-22 21:38:56.958 myApp[8923:f803] -- 308.000000 and 308.000000 
> 2011-12-22 21:38:56.959 myApp[8923:f803] -- 308.000000 and 308.000000 
> 2011-12-22 21:38:56.960 myApp[8923:f803] -- 320.000000 and 308.000000    <---
> 2011-12-22 21:38:56.975 myApp[8923:f803] -- 320.000000 and 320.000000 
> 2011-12-22 21:38:56.976 myApp[8923:f803] -- 320.000000 and 320.000000 
> 2011-12-22 21:38:56.977 myApp[8923:f803] -- 330.000000 and 320.000000 
> 2011-12-22 21:38:56.987 myApp[8923:f803] -- 330.000000 and 330.000000 
> 2011-12-22 21:38:56.988 myApp[8923:f803] -- 330.000000 and 330.000000 
> 2011-12-22 21:38:56.990 myApp[8923:f803] -- 339.000000 and 330.000000    <---
> 2011-12-22 21:38:56.999 myApp[8923:f803] -- 339.000000 and 339.000000 
> 2011-12-22 21:38:57.001 myApp[8923:f803] -- 339.000000 and 339.000000 
> 2011-12-22 21:38:57.002 myApp[8923:f803] -- 339.000000 and 339.000000 
> 2011-12-22 21:38:57.012 myApp[8923:f803] -- 339.000000 and 339.000000 
> 2011-12-22 21:38:57.013 myApp[8923:f803] -- 339.000000 and 339.000000 
> 2011-12-22 21:38:57.015 myApp[8923:f803] -- 349.000000 and 339.000000    <---