Cocoa Touch - 在屏幕上移动UIImageView

时间:2010-07-18 17:13:04

标签: cocoa-touch uiimageview

让用户拖动UIImageView以将其定位在他们选择的位置的简单方法是什么。这是一个摄影应用程序。

谢谢!

1 个答案:

答案 0 :(得分:2)

首先,制作一个能够处理触摸事件的容器视图。 然后,你需要一个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

希望它有所帮助!

相关问题