拖放而不删除UIButton [英] Drag and drop without removing UIButton
本文介绍了拖放而不删除UIButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
首先,我将操作分配给按钮:
[button addTarget:self action:@selector(imageMoved:withEvent :) forControlEvents:UIControlEventTouchDragInside];
然后,我创建拖动:
- (IBAction)imageMoved:(id)sender withEvent:(UIEvent *)event
{
NSLog(@moved);
CGPoint point = [[[all allTouches] anyObject] locationInView:self.view];
UIControl * control = sender;
control.center = point;
}
然而,我想要实现的是拖动的按钮不是从它的位置移除。所以我想到一个解决方案是创建一个类似于第一个新的按钮,并移动这个新创建的按钮。但是我在执行这个最后一部分时遇到麻烦。
任何帮助?非常感谢!
解决方案
- (IBAction)imageMoved:(id)sender withEvent: (UIEvent *)事件{
NSLog(@moved);
CGPoint point = [[[all allTouches] anyObject] locationInView:self.view];
if(![self.view viewWithTag:11]){
NSData * archivedData = [NSKeyedArchiver archivedDataWithRootObject:(UIButton *)sender];
UIButton * anotherButton =(UIButton *)[NSKeyedUnarchiver unarchiveObjectWithData:archivedData];
anotherButton.tag = 11;
UIImage * senderImage = [(UIButton *)sender imageForState:UIControlStateNormal];
CGImageRef cgImage = [senderImage CGImage];
UIImage * copyOfImage = [[UIImage alloc] initWithCGImage:cgImage];
[anotherButton setImage:copyOfImage forState:UIControlStateNormal];
[self.view addSubview:anotherButton];
}
[self.view viewWithTag:11] .center = point;
}
I'm implementing a drag and drop system in an iphone app. So far I managed to implement the draggable button and it is working fine. This is the code that I have:
First, I assign the action to the button:
[button addTarget:self action:@selector(imageMoved:withEvent:) forControlEvents:UIControlEventTouchDragInside];
then, I create the drag:
- (IBAction) imageMoved:(id) sender withEvent:(UIEvent *) event
{
NSLog(@"moved");
CGPoint point = [[[event allTouches] anyObject] locationInView:self.view];
UIControl *control = sender;
control.center = point;
}
However, what I want to achieve is that the button being dragged doesn't gets removed from where it is. So I thought of one solution that is to create a new button similar to the first and move the this newly created button. But I having trouble in implementing this last part. Any help? Thanks very much!
解决方案
- (IBAction) imageMoved:(id) sender withEvent:(UIEvent *) event {
NSLog(@"moved");
CGPoint point = [[[event allTouches] anyObject] locationInView:self.view];
if (![self.view viewWithTag:11]) {
NSData *archivedData = [NSKeyedArchiver archivedDataWithRootObject:(UIButton*)sender];
UIButton *anotherButton =(UIButton*) [NSKeyedUnarchiver unarchiveObjectWithData:archivedData];
anotherButton.tag = 11;
UIImage *senderImage=[(UIButton *)sender imageForState:UIControlStateNormal];
CGImageRef cgImage = [senderImage CGImage];
UIImage *copyOfImage = [[UIImage alloc] initWithCGImage:cgImage];
[anotherButton setImage:copyOfImage forState:UIControlStateNormal];
[self.view addSubview:anotherButton];
}
[self.view viewWithTag:11].center = point;
}
这篇关于拖放而不删除UIButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文