我正在编写一个可以在屏幕上拖动的自定义UIButton。当我按下此按钮时,uiview将被添加到superview中。我为这个视图添加了平移手势,所以它也可以拖动。
现在我想让uiview和按钮同时可以拖动。我该怎么办?
例如:
答案 0 :(得分:1)
我会将按钮和视图封装在父透明UIView中。将摇摄手势添加到它并移动它们......然后它们都会移动。
答案 1 :(得分:0)
我为你的应用程序尝试了示例代码。它工作正常。当你拖动按钮时,视图也会同时移动。
首先我创建了按钮和UIView.I连接了它。
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIButton *btnDragg;
- (IBAction)actionDragg:(id)sender;
@property (strong, nonatomic) IBOutlet UIView *viewDrag;
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize btnDragg,viewDrag;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIPanGestureRecognizer *panRecognizer;
panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(wasDragged:)];
panRecognizer.cancelsTouchesInView = YES;
[btnDragg addGestureRecognizer:panRecognizer];
}
- (void)wasDragged:(UIPanGestureRecognizer *)recognizer
{
UIButton *button = (UIButton *)recognizer.view;
CGPoint translation = [recognizer translationInView:button];
CGPoint translationView = [recognizer translationInView:viewDrag];
button.center = CGPointMake(button.center.x + translation.x, button.center.y + translation.y);
viewDrag.center = CGPointMake(viewDrag.center.x + translationView.x, viewDrag.center.y + translationView.y);
[recognizer setTranslation:CGPointZero inView:button];
[recognizer setTranslation:CGPointZero inView:viewDrag];
}