isicom
isicom

Reputation: 103

Touched Move UILabel works until the value is fix - Bug or Features?

a very strange result.

  1. Start a Single View Application
  2. Add a UILabel
  3. Put my Code in

    import "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. Connect label with

    __weak IBOutlet UILabel *textLable;

  5. let it RUN

Now you can move the label by touching moving.

Ok...and NOW!!!!

change the change

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

to

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

Run the app and try the move!!! you will see, label jumps between new and start position, if you start touched moving.

I tried by using a UIView as container....same thing: Once you change the value of moved object (UIButton same), moving is not working right.

Report to Appel is already send...no answere!!!

Is it a Bug or a features???

Upvotes: 0

Views: 65

Answers (1)

NSSakly
NSSakly

Reputation: 209

The idea is about your frame and your location touch. You are centering Label to your touch. So nothing is about setting the text. Try this code, detecting distance between touch and centre text when first touch and adding the center label to this point :

    @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

Upvotes: 1

Related Questions