Thursday, 8 August 2013

Creating a new object when a UIlabel is dragged and dropped on another using the Pan gesture recognizer

Creating a new object when a UIlabel is dragged and dropped on another
using the Pan gesture recognizer

i have a UIlabel on my view... i want to create another object when i drag
another UIlabel onto the previous UIlabel on the view. it should work in a
way that when i drag the label onto the previous one and drop it on the
previous UIlabel, it creates the object. the code i have doesnt recognize
the UIGestureStateEnded. it creates the object when i drag it across the
UIlabel. it doesnt wait for me to drop it on the UIlabel. this is the code
below. any help will be appreciated.
(void) panDetected:(UIPanGestureRecognizer *)paramSender{ BOOL
isRelationship; entity *mergedEntity; entity * currentSender=(entity*)
paramSender.view;
if (paramSender.state != UIGestureRecognizerStateEnded &&
paramSender.state != UIGestureRecognizerStateFailed) { CGPoint location =
[paramSender locationInView:paramSender.view.superview];
paramSender.view.center = location; currentSender.layer.borderColor =
[UIColor greenColor].CGColor; currentSender.layer.borderWidth = 3.0;
}
if (paramSender.state == UIGestureRecognizerStateEnded ) {
CGPoint location =
[paramSender locationInView:paramSender.view.superview];
paramSender.view.center = location;
CGRect frame1 = CGRectMake(location.x -
_diagramCanvas.frame.origin.x,location.y - _diagramCanvas.frame.origin.y,
currentSender.frame.size.width+SHAPE_OFFSETSIZE_X,
currentSender.frame.size.height+SHAPE_OFFSETSIZE_Y);
entity *newObject = [[entity alloc]initWithFrame:frame1];
newObject.text=currentSender.text; newObject.tag = 2; newObject.font =
currentSender.font; newObject.userInteractionEnabled = YES;
newObject.textAlignment = NSTextAlignmentCenter; [_diagramCanvas
addSubview:newObject]; newObject.backgroundColor = [UIColor brownColor];
UIPanGestureRecognizer *newPan = [[UIPanGestureRecognizer
alloc]initWithTarget:self action: @selector(panRecognized:)]; [newPan
setMaximumNumberOfTouches:1]; [self.view addGestureRecognizer:newPan];
[newObject addGestureRecognizer:newPan]; [currentSender
removeFromSuperview];
}
NSLog(@"I am here");
for(entity *eachComp in _diagramCanvas.subviews){
if((eachComp.tag==2 )){ if (CGRectIntersectsRect([currentSender.superview
convertRect:currentSender.frame toView:self.view], [eachComp.superview
convertRect:eachComp.frame toView:self.view])) { CGRect
currentSenderFrame=[currentSender.superview
convertRect:currentSender.frame toView:_diagramCanvas]; CGRect myViewsRect
= CGRectMake(currentSenderFrame.origin.x +80, currentSenderFrame.origin.y
+80, 80.0f, 80.0f); entity *newObject = [[entity alloc]
initWithFrame:myViewsRect ];
newObject.text=currentSender.text;
newObject.font = currentSender.font;
newObject.tag = 3;
newObject.adjustsFontSizeToFitWidth =YES;
newObject.userInteractionEnabled = YES;
newObject.textAlignment = NSTextAlignmentCenter;
newObject.backgroundColor = [UIColor redColor];
newObject.layer.cornerRadius = 37.5;
[_diagramCanvas addSubview:newObject];
UIPanGestureRecognizer *newPan = [[UIPanGestureRecognizer
alloc]initWithTarget:self action:
@selector(panRecognized:)];
[newPan setMaximumNumberOfTouches:1];
[newObject addGestureRecognizer:newPan];
[currentSender removeFromSuperview];

No comments:

Post a Comment