Какао Touch - перемещение UIImageView на экране - PullRequest
3 голосов
/ 18 июля 2010

Какой простой способ позволить пользователям перетаскивать UIImageView, чтобы расположить его в месте по своему выбору.Это для приложения фотографии.

Спасибо!

1 Ответ

2 голосов
/ 18 июля 2010

Сначала создайте контейнер, который будет заботиться о событиях касания.Затем вам нужен подкласс UIIMageView, который будет гарантировать, что когда вы устанавливаете центр объекта, он не выходит за пределы экрана.

Я создал такой класс некоторое время назад, воткод (он также может быть анимируемым).

.h

#import <UIKit/UIKit.h>

@interface ObjectView : UIImageView 
{
}

-(void) setCenter: (CGPoint) center inBounds: (CGRect) bounds withAnimation: (BOOL) animate;

@end

.m

#import "ObjectView.h"

@implementation ObjectView

#define MOVE_TO_CENTER_DURATION 0.1
-(void) setCenter: (CGPoint) center inBounds: (CGRect) bounds withAnimation: (BOOL) animate
{
    CGRect frame = self.frame;

    if(center.x + frame.size.width/2 > bounds.origin.x + bounds.size.width) 
        center.x -= ((center.x + frame.size.width/2) - (bounds.origin.x + bounds.size.width));
    else if(center.x - frame.size.width/2 < bounds.origin.x) 
        center.x += bounds.origin.x - (center.x - frame.size.width/2);

    if(center.y + frame.size.height/2 > bounds.origin.y + bounds.size.height) 
        center.y -= (center.y + frame.size.height/2) - (bounds.origin.y + bounds.size.height);
    else if(center.y - frame.size.height/2 < bounds.origin.y) 
        center.y += bounds.origin.y - (center.y - frame.size.height/2);

    if(animate) {
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration: MOVE_TO_CENTER_DURATION];
    }

    self.center = center;

    if(animate)
        [UIView commitAnimations];

} 

@end

Надеюсь, это немного поможет!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...