ios - 触摸 move UILabel 一直有效,直到值修复 - 错误还是功能?

标签 ios iphone xcode touch move

一个非常奇怪的结果。

  1. 启动单一 View 应用程序
  2. 添加一个 UILabel
  3. 把我的代码放进去

    导入“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
    
  4. 连接标签

    __weak IBOutlet UILabel *textLable;

  5. 让它运行

现在您可以通过触摸 move 来 move 标签。

好的...现在!!!!

改变改变

 // 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; //<------------------------------

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

我尝试使用 UIView 作为容器......同样的事情:一旦你改变了 move 对象的值(UIButton 相同), move 就无法正常工作。

向 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/

相关文章:

ios - 如何设置自定义collectionView Cell Frame?

ios - CKRecordZone 中的记录仍然显示在 CloudKit 仪表板中

ios - iOS联系人备份我们需要保存的文件扩展名

iphone - NSURLConnection 下载委托(delegate)方法未被调用

iphone - Google Analytics 独特购买突然停止工作

iOS - 为自定义键盘播放 Tock 声音

iphone - 如何在不占据整个屏幕的 UIView 上使用 UIModalTransitionStylePartialCurl?

iphone - 具有非 GPL 许可的 iOS 的 SIP 库

objective-c - 在一个简单的应用程序中使用大量的 xib 有什么问题吗?

ios - 通知服务扩展签名证书