一个非常奇怪的结果。


启动单视图应用程序
添加一个UILabel
输入我的代码

导入“ ViewController.h”

@interface ViewController ()
{
   float oldX, oldY;
bool dragging;
__weak IBOutlet UILabel *textLable;
}
@end

@implementation ViewController

- (void)viewDidLoad {
   [super viewDidLoad];
   // Do any additional setup after loading the view, typically from a nib.
   // self.textLable.backgroundColor = [UIColor clearColor];
}

- (void)didReceiveMemoryWarning {
   [super didReceiveMemoryWarning];
   // Dispose of any resources that can be recreated.
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    // get touch event
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchLocation = [touch locationInView:self.view];
    if ([touch view] == textLable)
    {
       int intValue =  (int)((touchLocation.y * 24.0)/300.0);
       NSString *anyValue = [NSString stringWithFormat:@"%d",intValue];

       textLable.center= touchLocation;

       // RUN WITHOUT THIS PART .... tochedMoved workt great
       //
       // Run With this PART ..... touchedMoved is damaged!!!!!
       //textLable.text = anyValue;
       NSLog(@"%f %f", touchLocation.y, textLable.frame.origin.y);
    }
}
@end

将标签与

__weak IBOutlet UILabel * textLable;
让它运行


现在,您可以通过触摸移动来移动标签。

好吧...现在!!!!

改变改变

 // RUN WITHOUT THIS PART .... tochedMoved workt great
 //
 // Run With this PART ..... touchedMoved is damaged!!!!!
 //textLable.text = anyValue;




 // RUN WITHOUT THIS PART .... tochedMoved workt great
 //
 // Run With this PART ..... touchedMoved is damaged!!!!!
 textLable.text = anyValue; //<------------------------------


运行该应用程序并尝试移动!!!您会看到,如果您开始触摸移动,标签就会在新位置和开始位置之间跳转。

我尝试通过使用UIView作为容器...。同样的事情:一旦更改了移动对象的值(UIButton相同),移动就无法正常进行。

向Appel的报告已发送...没有答复!!!

是Bug还是功能???

最佳答案

这个想法是关于您的框架和位置触摸。您将Label置于触摸的中心。因此,与设置文本无关。
尝试以下代码,在首次触摸时检测触摸和中心文本之间的距离,并在此点添加中心标签:

    @interface ViewController ()
{
    float distanceTouchX, distanceTouchY;
    bool dragging;
    CGPoint firstLocation;
    __weak IBOutlet UILabel *textLable;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchLocation = [touch locationInView:self.view];

    distanceTouchX = textLable.center.x - touchLocation.x;
    distanceTouchY = textLable.center.y - touchLocation.y;

}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    // get touch event
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchLocation = [touch locationInView:self.view];
    if ([touch view] == textLable)
    {
        int intValue =  (int)((touchLocation.y * 24.0)/320.0);

        NSString *anyValue = [NSString stringWithFormat:@"%d",intValue];

        CGPoint newTouchLocation = touchLocation;
        newTouchLocation.x = newTouchLocation.x + distanceTouchX;
        newTouchLocation.y = newTouchLocation.y + distanceTouchY;

        textLable.center= newTouchLocation;
        textLable.text = anyValue;
        NSLog(@"%f %f", touchLocation.y, textLable.frame.origin.y);
    }
}
@end

关于ios - 触摸Move UILabel一直有效直到该值被修正-错误或功能?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28085197/

10-10 05:57
查看更多