2011-01-27 15 views
5

UIView alt sınıfım var ve süper görüntüde dolaşabiliyorum. Kullanıcı UIView'e self.center dışında bir yere dokunduğunda ancak self.bounds içinde "atlar", çünkü gerçek hareketi elde etmek için self.center'a yeni konumu ekliyorum. Bu davranıştan kaçınmak için, kullanıcının görünümü sınırlar içinde herhangi bir yere çekmesine ve sürüklemesine izin veren bir bağlantı noktası ayarlamaya çalışıyorum.UIView katmanı için bağlantı noktası belirleme

Sorunum şu ki, yeni bağlantı noktasını (aşağıdaki kodda gösterildiği gibi) hesapladığımda hiçbir şey olmuyor, görünüm hiç değişmiyor. Diğer yandan, çapa noktasını önceden hesaplanmış bir noktaya ayarlarsam, görünümü taşıyabilirim (ama daha sonra elbette önceden hesaplanan noktaya "atlar"). Bu nasıl beklendiği gibi çalışmıyor?

Teşekkürler.

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    // Only support single touches, anyObject retrieves only one touch 
    UITouch *touch = [touches anyObject]; 
    CGPoint locationInView = [touch locationInView:self]; 

    // New location is somewhere within the superview 
    CGPoint locationInSuperview = [touch locationInView:self.superview]; 

    // Set an anchorpoint that acts as starting point for the move 
    // Doesn't work! 
    self.layer.anchorPoint = CGPointMake(locationInView.x/self.bounds.size.width, locationInView.y/self.bounds.size.height); 
    // Does work! 
    self.layer.anchorPoint = CGPointMake(0.01, 0.0181818); 

    // Move to new location 
    self.center = locationInSuperview; 
} 
+1

anchorPoint'i tam olarak anladığınızdan emin olun: http://stackoverflow.com/a/22188420/550393 – 2cupsOfTech

cevap

12

Kris Van Bael'in belirttiği gibi, hareketi engellememek için touchsBegan:withEvent: yönteminde çapa noktası hesaplamalarını yapmanız gerekecektir. Ek olarak, katmanın anchorPoint şeklini değiştirdiğinizde, görünümün ilk konumunu taşıyacağından, ilk dokunuştan sonra 'atlama' yapmaktan kaçınmak için görüntünün center noktasına bir uzaklık eklemeniz gerekir.

Sen hesaplanması (ve görünümün center noktasına ekleme) ilk ve son anchorPoints arasındaki farka dayalı offset bunu yapabilirsiniz (görünümünüzün genişlik/yükseklik ile çarpılır) ya da görünüm en center ilk olarak ayarlanmış olabilir temas noktası. belki böyle

şey:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 

    UITouch *touch = [touches anyObject]; 
    CGPoint locationInView = [touch locationInView:self]; 
    CGPoint locationInSuperview = [touch locationInView:self.superview]; 

    self.layer.anchorPoint = CGPointMake(locationInView.x/self.frame.size.width, locationInView.y/self.frame.size.height); 
    self.center = locationInSuperview; 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 

    UITouch *touch = [touches anyObject]; 
    CGPoint locationInSuperview = [touch locationInView:self.superview]; 

    self.center = locationInSuperview; 
} 

Daha elma dokümanlar here den anchorPoint 's üzerinde bilgi ve here başvurulan benzer SO soru.

0

Yalnızca touchBegin üzerinde anchorpoint güncellemeniz gerekir. Her zaman yeniden hesaplarsanız (TouchMoved), alt görüntünün hareket etmemesi mantıklıdır.

+0

Elbette haklısınız! Ayrıca, kullanıcı, fiili hareket başladığı zaman, kullanıcının parmağın sınırları dahilinde hareket etmeyeceği için, tutturma noktasının yeniden hesaplanmasında herhangi bir nokta yoktur. AnchorPoint'in güncellemesini touhesBegan ile değiştirmek, Sam'in cevabında vurguladığı gibi bir ofset problemi ortaya çıkardı. – Oskar

İlgili konular