以编程方式创建移动uiimageview [英] Moving uiimageview created programmatically
本文介绍了以编程方式创建移动uiimageview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我编写了以下代码但它不起作用。图片出现但我无法移动
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
@interface ViewController : UIViewController
{
IBOutlet UIImageView *oneEl;
}
@property (nonatomic,retain) IBOutlet UIImageView *oneEl;
.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;
}
}
它有效!
我怎样才能使uiimageview以编程方式创建?
And it works! How can I make so that programmatically created by uiimageview could move?
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;
}
}
}
推荐答案
像下面的代码一样初始化你的UIImageView然后它会像你的第二个例子一样工作:
Initialize your UIImageView like the following code and then it will work like your second example:
ViewController.h
ViewController.h
@interface ViewController : UIViewController
{
UIImageView *oneselement;
}
@property (nonatomic,retain) UIImageView *oneselement;
ViewController.m
ViewController.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;
}
}
这篇关于以编程方式创建移动uiimageview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文