gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 02:21:28 25 4
gpt4 key购买 nike

一个非常奇怪的结果。

  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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com