• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

objective-c - 从固定的 UIImageView 或按钮中拖出新的 UIImageView 对象

[复制链接]
菜鸟教程小白 发表于 2022-12-12 21:11:09 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我是 iOS 开发的初学者,我正在为我的学校做一个业余项目。我想做的是有一个小图像,用户可以从中拖出更多图像。本质上,我有一个应用程序,我正在生成方 block ,我需要有一个固定方 block ,用户可以在其中触摸方 block 并将新方 block 拖到板上。我已经实现了这一点,但这还不是理想的行为。目前,用户必须先触摸按钮然后松开,然后才能与新方 block 进行交互以将其拖离按钮。我想要的是让触摸事件以某种方式传递给创建的新 UIImageView 对象,这样它就可以无缝地拖出一个新的正方形。以下是我当前的代码。

//UIViewController 
#import <UIKit/UIKit.h>
#import "Tile.h"

@interface MenuViewController : UIViewController
{
    Tile *tileObject;
}

@end

#import "MenuViewController.h"

@implementation MenuViewController

- (IBAction)createTileUIButton *)sender {
    CGFloat tileX = sender.center.x;
    CGFloat tileY = sender.center.y;
    tileObject = [[Tile alloc]initWithX:tileX andY:tileY];
    [self.view addSubview:tileObject];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientationUIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return YES;
}

@end




//Custom Tile Class
#import <UIKit/UIKit.h>

@interface Tile : UIImageView
{
    CGPoint startLocation; 
}

- (id)init; //Default Initialization
- (id)initWithXCGFloat)x andYCGFloat)y; //Initialization with coordinate points from single screen tap

@end

#import "Tile.h"

@implementation Tile

//Default initialization
-(id) init 
{
    self = [self initWithX:0 andY:0];
    return self;
}

//Initialization with coordinate points from single screen tap
- (id)initWithXCGFloat)centerX andYCGFloat)centerY
{
    //Creates a UIImageView object from an image file
    UIImage *image = [UIImage imageNamed"redblock.png"];
    self = [super initWithImage:image];
    //Used to center the image under the single screen tap
    centerX -= (image.size.height/2);
    centerY -= (image.size.height/2);
    //Sets the position of the image
    self.frame = CGRectMake(centerX, centerY, image.size.width, image.size.height);
    self.userInteractionEnabled = YES; //required for interacting with UIImageViews
    return self;
}

/* Methods from Stack Overflow
http://stackoverflow.com/questions/1991899/uiimage-detecting-touch-and-dragging
Referenced from http://www.iphoneexamples.com/
*/
- (void) touchesBeganNSSet*)touches withEventUIEvent*)event {
    // Retrieve the touch point
    CGPoint point = [[touches anyObject] locationInView:self];
    startLocation = point;
    [[self superview] bringSubviewToFront:self];
}
- (void) touchesMovedNSSet*)touches withEventUIEvent*)event {
    // Move relative to the original touch point
    CGPoint point = [[touches anyObject] locationInView:self];
    CGRect frame = [self frame];
    frame.origin.x += point.x - startLocation.x;
    frame.origin.y += point.y - startLocation.y;
    [self setFrame:frame];
}
/* 
end of methods from Stack Overflow
*/

@end



Best Answer-推荐答案


如果初始触地点可以是一个 ImageView ,其中您要拖动的图像作为其图像,那么您可以这样做:

在您的 menuViewController 类中,将其放入 viewDidLoad(并删除按钮及其方法):

- (void)viewDidLoad
{
    [super viewDidLoad];
    tileObject = [[Tile alloc]initWithX:160 andY:200];
    [self.view addSubview:tileObject];
}

然后在你的 Tile 类中,在 touchesBegan:withEvent: 方法中创建一个新的 tile,并像以前一样执行拖动操作:

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
    Tile *newTile = [[Tile alloc]initWithX:160 andY:200];
    [self.superview addSubview:newTile];

    // Retrieve the touch point
    CGPoint point = [[touches anyObject] locationInView:self];
    startLocation = point;
    [[self superview] bringSubviewToFront:self];
}

关于objective-c - 从固定的 UIImageView 或按钮中拖出新的 UIImageView 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10740272/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap