tracking the position of dragview
- by user262325
Hello everyone
I put a dragview on an UIView, I hope when I drag the dragview, its superview (UIView) tracks the position of the dragview and moves itself to the position where to keep dragview at the original position on the UIView.
The codes show below:
#import <UIKit/UIKit.h>
@interface DragView : UIImageView {
CGPoint startLocation;
}
@end
#import "DragView.h"
@implementation DragView
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// Initialization code
}
return self;
}
- (void)drawRect:(CGRect)rect {
// Drawing code
}
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event
{
CGPoint pt=[[touches anyObject] locationInView:self];
startLocation=pt;
[[self superview] bringSubviewToFront:self];
}
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint pt=[[touches anyObject] locationInView:self];
CGRect frame=[self frame];
frame.origin.x+=pt.x-startLocation.x;
frame.origin.y+=pt.y-startLocation.y;
CGRect frame1=[[self superview] frame];//superview of the dragview
frame1.origin.x+=frame.origin.x; //tracks the postion of dragview
frame1.origin.y+=frame.origin.y;
[[self superview] setFrame: frame1 ]; //move to new position
[self setFrame:frame];
}
my problem is that the UIView moved but lagged to the movement of the drageview(RED BLOCK).
Welcome any comment
Thanks
interdev