画面上でUIPanGesturerecogniser
移動するために使用しています。UIButton
この方法でボタンにジェスチャを追加しています。
panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[ButtonNote addGestureRecognizer:panRecognizer];
そして方法は
- (void)move:(id)sender {
[[[(UITapGestureRecognizer*)sender view] layer] removeAllAnimations];
CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.ViewA];
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {
firstX = [[sender view] center].x;
firstY = [[sender view] center].y;
[self.view addSubview:[(UIPanGestureRecognizer*)sender view]];
if (self.Button1.frame.size.height > 200) {
}
else {
}
}
translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY+translatedPoint.y);
[[sender view] setCenter:translatedPoint];
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded||[(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateCancelled||[(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateFailed){
CGPoint fingerPoint2 = [(UIPanGestureRecognizer*)sender locationInView:self.BallBin.superview];
if (CGRectContainsPoint(self.BallBin.frame, fingerPoint2)) {
if (self.Button3.frame.size.height > 200) {
UIPanGestureRecognizer *gesture = (UIPanGestureRecognizer *)sender;
UIButton *button = (UIButton *)gesture.view;
[[(UIPanGestureRecognizer*)sender view] removeFromSuperview];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
NSString *myFilePath = [documentsDirectoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@", button.titleLabel.text]];
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtPath:myFilePath error:NULL];
[self performSelector:@selector(DeleteBusinessCard:) withObject:nil afterDelay:0.5];
}
else {
[self.ViewA addSubview:[(UIPanGestureRecognizer*)sender view]];
}
}
if (self.Button1.frame.size.height > 200) {
}
else {
[self.ViewB addSubview:self.BallBin];
}
}];
}
CGFloat finalX = translatedPoint.x + (.35*[(UIPanGestureRecognizer*)sender velocityInView:self.ViewA].x);
CGFloat finalY = translatedPoint.y + (.35*[(UIPanGestureRecognizer*)sender velocityInView:self.ViewA].y);
if(UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation])) {
if(finalX < 0) {
finalX = 0;
}
else if(finalX > 768) {
finalX = 768;
}
if(finalY < 0) {
finalY = 0;
}
else if(finalY > 1024) {
finalY = 1024;
}
}
else {
if(finalX < 0) {
finalX = 0;
}
else if(finalX > 1024) {
finalX = 768;
}
if(finalY < 0) {
finalY = 0;
}
else if(finalY > 768) {
finalY = 1024;
}
}
}
}
ボタンの動きを、画面上の特定の円領域、たとえば半径 80 に制限したいと考えています。これで、ボタンが画面全体に移動します。とにかくViewAはUIView
画面全体をカバーしています。