あなたがすべきことは、サブクラス化してそこにUIImageView
実装するtouchesMoved:
ことです。したがって、ドラッグ可能なビューを初期化すると、両方ともtouchesMoved:
機能を継承します。コードは次のようになります...
//Player.h
@interface Player : UIImageView
CGPoint startLocation;
@end
//Player.m
@implementation Player
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
// Retrieve the touch point
CGPoint pt = [[touches anyObject] locationInView:self];
startLocation = pt;
}
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint pt = [[touches anyObject] locationInView:self];
CGFloat dx = pt.x - startLocation.x;
CGFloat dy = pt.y - startLocation.y;
CGPoint newCenter = CGPointMake(self.center.x + dx, self.center.y + dy);
self.center = newCenter;
}
@end
を初期化するPlayer
と、以下の例が表示されます。
Player *player1 = [[Player alloc] initWithImage:[UIImage imageNamed:@"player1.png"]];
[self.view addSubview:player1];
// You can now drag player1 around your view.
Player *player2 = [[Player alloc] init];
[self.view addSubview:player2];
// You can now drag player2 around your view.
Players
これらをあなたUIViewController
のビューに追加していると仮定します。
どちらも実装-touchesMoved:
お役に立てれば !
UPDATE : -touchesBegan:
Dragging a subclass の完全な例が追加されました。これはデフォルトでオフになっているため、プロパティをUIImageView
必ずYESに設定してください。.userInteractionEnabled