xcode 移动以编程方式创建的 uiimageview

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11541130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:55:53  来源:igfitidea点击:

Moving uiimageview created programmatically

iphoneobjective-cxcodeios5

提问by JinDeveloper

I wrote the following code and it does not work. Picture appears but I can not move it

我写了下面的代码,但它不起作用。图片出现但是我不能移动它

UIImageView *oneselement = [[UIImageView alloc] initWithFrame:self.view.frame];
oneselement = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1el.png"]];
oneselement.frame = CGRectMake(0, 0, 122, 122);
[self.view addSubview:oneselement];


-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];

    if([touch view]==oneselement)
    {
        CGPoint location = [touch locationInView:self.view];
        oneselement.center=location;
    }}

I added a picture to view and wrote the following code

我加了一张图片查看,写了如下代码

.h

。H

@interface ViewController : UIViewController
{
    IBOutlet UIImageView *oneEl;
}
@property (nonatomic,retain) IBOutlet UIImageView *oneEl;

.m

.m

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
    if([touch view]==oneEl)
    {
        CGPoint location = [touch locationInView:self.view];
        oneEl.center=location;
    }
}

And it works! How can I make so that programmatically created by uiimageview could move?

它有效!我怎样才能让 uiimageview 以编程方式创建的可以移动?

NSMUTABLEARRAY

可变数组

self.elements=[myElements getElements];

imagesElements = [[NSMutableArray alloc]init];
for(ElemetsList *item in self.elements)
{
        UIImageView *oneelement = [[UIImageView alloc] initWithImage:[UIImage imageNamed:item.imgElement]];
        oneelement.frame = CGRectMake(item.positionX, item.positionY, item.width, item.height);
        oneelement.userInteractionEnabled=YES;
        [imagesElements addObject:oneelement];
}

for(UIImageView *img in imagesElements) 
    [self.view addSubview:img];

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];

    for(UIImageView *img in imagesElements) 
    {  
        if([self view] == img)
        {
            CGPoint location = [touch locationInView:self.view];
            img.center=location;
        }
    }
}

回答by tikhop

Initialize your UIImageView like the following code and then it will work like your second example:

像下面的代码一样初始化你的 UIImageView,然后它会像你的第二个例子一样工作:

ViewController.h

视图控制器.h

@interface ViewController : UIViewController
{
    UIImageView *oneselement;
}
@property (nonatomic,retain) UIImageView *oneselement;

ViewController.m

视图控制器.m

//initialize your UIImageView
UIImageView *newOneselement = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1el.png"]];
self.oneselement = newOneselement;
self.oneselement.userInteractionEnabled = YES;
self.oneselement.frame = CGRectMake(0, 0, 122, 122);
[self.view addSubview:self.oneselement];
[newOneselement release];

//Handle touchesMoved
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];
    if([touch view]==self.oneselement)
    {
        CGPoint location = [touch locationInView:self.view];
        self.oneselement.center=location;
    }
}

回答by Nitish

Please see thislink.
You can also try :

请看这个链接。
您也可以尝试:

In order to move an image, you should enclose the code to move in an animation block:

为了移动图像,您应该在动画块中包含要移动的代码:

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
tapImage.center = CGPointMake(156, 110);
[UIView commitAnimations];

You can also create a method to execute upon completion of the animation with the UIView setAnimationDidStopSelector: method.

您还可以使用 UIView setAnimationDidStopSelector: 方法创建一个在动画完成时执行的方法。

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animateImages)];
tapImage.center = CGPointMake(156, 110);
[UIView commitAnimations];

//other stuff

-(void)animateImages{
     [tapImage startAnimating];
}