Ser*_*nce 4 cocoa-touch core-graphics uiview ios
给定两个UIViews带有UIPanGestureRecognizers附加到它们的边框:
如果我将UIView左边的拖到UIView右边,这是通常的行为:
寻找最简单的方法来做到这一点!
Dim*_*rov 10
一种方法是使用多个同级图层和zPosition. 为了达到效果,您添加了两层,一层用于边框,一层用于内容。并且边框层zPosition比内容小。而且,当然,使用UIPanGestureRecognizer.
迅速:
import UIKit
class MergingView: UIView {
    let borderLayer = CALayer()
    let backgroundLayer = CALayer()
    override func layoutSubviews() {
        super.layoutSubviews()
        addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(handlePan(_:))))
        borderLayer.borderWidth = 5
        borderLayer.frame = frame
        borderLayer.zPosition = 10
        borderLayer.borderColor = UIColor.black.cgColor
        superview?.layer.addSublayer(borderLayer)
        backgroundLayer.frame = CGRect(x: frame.origin.x + 5, y: frame.origin.y + 5, width: frame.width - 10, height: frame.height - 10)
        backgroundLayer.zPosition = 20
        backgroundLayer.backgroundColor = UIColor.white.cgColor
        superview?.layer.addSublayer(backgroundLayer);
    }
    @objc func handlePan(_ recognizer: UIPanGestureRecognizer) {
        CATransaction.begin()
        CATransaction.setValue(kCFBooleanTrue, forKey: kCATransactionDisableActions)
        let translation = recognizer.translation(in: self)
        frame = self.frame.offsetBy(dx: translation.x, dy: translation.y)
        recognizer.setTranslation(CGPoint.zero, in: self)
        borderLayer.frame = borderLayer.frame.offsetBy(dx: translation.x, dy: translation.y)
        backgroundLayer.frame = backgroundLayer.frame.offsetBy(dx: translation.x, dy: translation.y)
        CATransaction.commit()
    }
}
Objective-C 头文件:
#import <UIKit/UIKit.h>
@interface MVMergingView : UIView
@end
Objective-C 实现:
#import "MVMergingView.h"
@interface MVMergingView ()
@property (strong) CALayer *borderLayer;
@property (strong) CALayer *backgroundLayer;
@end
@implementation MVMergingView
- (void)layoutSubviews {
    [super layoutSubviews];
    [self addGestureRecognizer:[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)]];
    CALayer *borderLayer = [CALayer layer];
    borderLayer.borderWidth = 5.f;
    borderLayer.frame = self.frame;
    borderLayer.zPosition = 10;
    borderLayer.borderColor = UIColor.blackColor.CGColor;
    self.borderLayer = borderLayer;
    [self.superview.layer addSublayer:borderLayer];
    CALayer *backgroundLayer = [CALayer layer];
    backgroundLayer.frame = CGRectMake(self.frame.origin.x + 5.f, self.frame.origin.y + 5.f, self.frame.size.width - 10, self.frame.size.height - 10);
    backgroundLayer.zPosition = 20;
    backgroundLayer.backgroundColor = UIColor.whiteColor.CGColor;
    self.backgroundLayer = backgroundLayer;
    [self.superview.layer addSublayer:backgroundLayer];
}
- (void)handlePan:(UIPanGestureRecognizer *)recognizer {
    [CATransaction begin];
    [CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
    CGPoint translation = [recognizer translationInView:self];
    self.frame = CGRectOffset(self.frame, translation.x, translation.y);
    [recognizer setTranslation:CGPointZero inView:self];
    self.borderLayer.frame = CGRectOffset(self.borderLayer.frame, translation.x, translation.y);
    self.backgroundLayer.frame = CGRectOffset(self.backgroundLayer.frame, translation.x, translation.y);
    [CATransaction commit];
}
@end
示例存储库:https : //github.com/dimitarnestorov/MergingView
| 归档时间: | 
 | 
| 查看次数: | 263 次 | 
| 最近记录: |