English 中文(简体)
如何将图像缩放到我双倍点击的地方?
原标题:How to get the image to zoom where I double tapped?

我设法得到了我的应用程序 放大我的形象,如果我双倍点击它, 但它使 t 缩放 在那里我双倍点击! 我希望图像能集中 我双倍点击的坐标!

我的代码:

.h 文件 :

 - (void)handleDoubleTap:(UIGestureRecognizer *)gestureRecognizer;

.m 文件 :

    UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];

[doubleTap setNumberOfTapsRequired:2];

[self.scroll addGestureRecognizer:doubleTap];

- (void)handleDoubleTap:(UIGestureRecognizer *)gestureRecognizer {  

    if(self.scroll.zoomScale > scroll.minimumZoomScale)
        [self.scroll setZoomScale:scroll.minimumZoomScale animated:YES]; 
    else 
        [self.scroll setZoomScale:1.6 animated:YES]; 
 }

接下来我该怎么办?

提前感谢!

/一个noob

问题回答

here is a snippet from my old project.
Add the UITapGestureRecognizer somewhere in your init-method:

UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doubleTapRecognized:)];
doubleTap.numberOfTapsRequired = 2;
[self addGestureRecognizer:doubleTap];
[doubleTap release];

在你看来,这双击枪在某个地方被射杀

- (void)doubleTapRecognized:(UITapGestureRecognizer*)recognizer {
    [self zoomToPoint:[recognizer locationInView:content]];
}

- (void)zoomToPoint:(CGPoint)location {
    float zoomScaleBefore = self.zoomScale;

    if (location.x<=0) location.x = 1;
    if (location.y<=0) location.y = 1;
    if (location.x>=content.bounds.size.width) location.x = content.bounds.size.width-1;
    if (location.y>=content.bounds.size.height) location.y = content.bounds.size.height-1;

    float percentX = location.x/content.bounds.size.width;
    float percentY = location.y/content.bounds.size.height;


    [self setZoomScale:10.0 animated:YES];

    float pox = (percentX*self.contentSize.width)-(self.frame.size.width/2);
    float poy = (percentY*self.contentSize.height)-(self.frame.size.height/2);

    if (pox<=0) pox = 1;
    if (poy<=0) poy = 1;

    [self scrollRectToVisible:
     CGRectMake(pox, poy, self.frame.size.width, self.frame.size.height)
                     animated:(self.zoomScale == zoomScaleBefore)];
}

双击缩放的 < strong> Swift 3. 0 版本 :

let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(onDoubleTap(gestureRecognizer:)))
tapRecognizer.numberOfTapsRequired = 2
view.addGestureRecognizer(tapRecognizer)

.

func onDoubleTap(gestureRecognizer: UITapGestureRecognizer) {
    let scale = min(scrollView.zoomScale * 2, scrollView.maximumZoomScale)

    if scale != scrollView.zoomScale {
        let point = gestureRecognizer.location(in: imageView)

        let scrollSize = scrollView.frame.size
        let size = CGSize(width: scrollSize.width / scale,
                          height: scrollSize.height / scale)
        let origin = CGPoint(x: point.x - size.width / 2,
                             y: point.y - size.height / 2)
        scrollView.zoom(to:CGRect(origin: origin, size: size), animated: true)
        print(CGRect(origin: origin, size: size))
    }
}

您正在寻找 < code>- placeInView: 。 它会在指定的视图中给出触碰发生位置的点 。 此时您可以调整视图, 使该点成为中心 。





相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签