菜鸟教程小白 发表于 2022-12-12 21:11:09

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


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

<pre><code>//UIViewController
#import &lt;UIKit/UIKit.h&gt;
#import &#34;Tile.h&#34;

@interface MenuViewController : UIViewController
{
    Tile *tileObject;
}

@end

#import &#34;MenuViewController.h&#34;

@implementation MenuViewController

- (IBAction)createTile:(UIButton *)sender {
    CGFloat tileX = sender.center.x;
    CGFloat tileY = sender.center.y;
    tileObject = [initWithX:tileX andY:tileY];
    ;
}

- (void)didReceiveMemoryWarning
{
    ;
    // Release any cached data, images, etc that aren&#39;t in use.
}

#pragma mark - View lifecycle

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

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

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return YES;
}

@end




//Custom Tile Class
#import &lt;UIKit/UIKit.h&gt;

@interface Tile : UIImageView
{
    CGPoint startLocation;
}

- (id)init; //Default Initialization
- (id)initWithX:(CGFloat)x andY:(CGFloat)y; //Initialization with coordinate points from single screen tap

@end

#import &#34;Tile.h&#34;

@implementation Tile

//Default initialization
-(id) init
{
    self = ;
    return self;
}

//Initialization with coordinate points from single screen tap
- (id)initWithX:(CGFloat)centerX andY:(CGFloat)centerY
{
    //Creates a UIImageView object from an image file
    UIImage *image = ;
    self = ;
    //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) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
    // Retrieve the touch point
    CGPoint point = [ locationInView:self];
    startLocation = point;
    [ bringSubviewToFront:self];
}
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
    // Move relative to the original touch point
    CGPoint point = [ locationInView:self];
    CGRect frame = ;
    frame.origin.x += point.x - startLocation.x;
    frame.origin.y += point.y - startLocation.y;
    ;
}
/*
end of methods from Stack Overflow
*/

@end
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>如果初始触地点可以是一个 ImageView ,其中您要拖动的图像作为其图像,那么您可以这样做:</p>

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

<pre><code>- (void)viewDidLoad
{
    ;
    tileObject = [initWithX:160 andY:200];
    ;
}
</code></pre>

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

<pre><code>- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
    Tile *newTile = [initWithX:160 andY:200];
    ;

    // Retrieve the touch point
    CGPoint point = [ locationInView:self];
    startLocation = point;
    [ bringSubviewToFront:self];
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于objective-c - 从固定的 UIImageView 或按钮中拖出新的 UIImageView 对象,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/10740272/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/10740272/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: objective-c - 从固定的 UIImageView 或按钮中拖出新的 UIImageView 对象