lemanhtien / MTSlideToOpen

A simple SlideToUnlock iOS UI component
MIT License
145 stars 51 forks source link

How to revert back unlock #14

Open Ammi-ulya opened 4 years ago

Ammi-ulya commented 4 years ago

After we swipe a button from left to right...how to reset it again to same position before it got swiped?

Thanks in advance

lemanhtien commented 4 years ago

Hi @Ammi-ulya , You can use the function resetStateWithAnimation. For more information, you can check in the project example 😄

Ammi-ulya commented 4 years ago

Yeah got it saw the example code.. thanks

can we programmatically swipe a button??? thanks in advance

On Mon, Dec 9, 2019 at 3:57 PM Martin notifications@github.com wrote:

Hi @Ammi-ulya https://github.com/Ammi-ulya , You can use the function resetStateWithAnimation. For more information, you can check in the project example 😄

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/lemanhtien/MTSlideToOpen/issues/14?email_source=notifications&email_token=AN7XZGBPNTEKTZAF32SQL5TQXYMQXA5CNFSM4JYGJ5TKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEGITZVY#issuecomment-563166423, or unsubscribe https://github.com/notifications/unsubscribe-auth/AN7XZGEKUIJI7BMRKFDHW63QXYMQXANCNFSM4JYGJ5TA .

lemanhtien commented 4 years ago

Hi @Ammi-ulya , The swiping a button programmatically is not supported in this version 😄

Ammi-ulya commented 4 years ago

okay.. all the best for your future implementations.

On Mon, Dec 9, 2019 at 4:08 PM Martin notifications@github.com wrote:

Hi @Ammi-ulya https://github.com/Ammi-ulya , The swiping a button programmatically is not supported in this version 😄

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/lemanhtien/MTSlideToOpen/issues/14?email_source=notifications&email_token=AN7XZGFSFNMK2RDN5CAYCA3QXYNY5A5CNFSM4JYGJ5TKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEGIVEVA#issuecomment-563171924, or unsubscribe https://github.com/notifications/unsubscribe-auth/AN7XZGH5AHHAPRD7OH52DPTQXYNY5ANCNFSM4JYGJ5TA .

Ammi-ulya commented 4 years ago

How to change the width of slide.thumnailImageView???

On Mon, Dec 9, 2019 at 4:28 PM Amulya H N amulya@codgrey.com wrote:

okay.. all the best for your future implementations.

On Mon, Dec 9, 2019 at 4:08 PM Martin notifications@github.com wrote:

Hi @Ammi-ulya https://github.com/Ammi-ulya , The swiping a button programmatically is not supported in this version 😄

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/lemanhtien/MTSlideToOpen/issues/14?email_source=notifications&email_token=AN7XZGFSFNMK2RDN5CAYCA3QXYNY5A5CNFSM4JYGJ5TKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEGIVEVA#issuecomment-563171924, or unsubscribe https://github.com/notifications/unsubscribe-auth/AN7XZGH5AHHAPRD7OH52DPTQXYNY5ANCNFSM4JYGJ5TA .

lemanhtien commented 4 years ago

You can change the thumbnailViewTopDistance value

SharmaVinay commented 4 years ago

how we can go back with same like swipe to lock again ? I want to do lock unlock with swipe not direct reset, please help.

ghost commented 4 years ago

Same as SharmaVinay, how we can go back with same like swipe to lock again ? I want to do lock unlock with swipe not direct reset, please help.

ghost commented 4 years ago

SharmaVinay have you found the solution? please let me know. thanks

SharmaVinay commented 4 years ago

yes I found it, I did some modifications and achieve it, but need to find that code, because in my app the requirement changed, I will find it and share with you give me some time.

ghost commented 4 years ago

Sure, thanks in advance, I am waiting.

SharmaVinay commented 4 years ago

@pankaj-rana01 you can use as per your requirement.

import UIKit

@objc public protocol MTSlideToOpenDelegate { func mtSlideToOpenDelegateDidFinish( sender: MTSlideToOpenView) func mtSlideToCloseDelegateDidFinish( sender: MTSlideToOpenView) }

@objcMembers public class WSSlideToOpenView: UIView { // MARK: All Views public let textLabel: UILabel = { let label = UILabel.init() return label }() public let sliderTextLabel: UILabel = { let label = UILabel() return label }() public let thumnailImageView: UIImageView = { let view = WSRoundImageView() view.isUserInteractionEnabled = true view.contentMode = .center return view }() public let sliderHolderView: UIView = { let view = UIView() return view }() public let draggedView: UIView = { let view = UIView() return view }() public let view: UIView = { let view = UIView() return view }() // MARK: Public properties public weak var delegate: WSSlideToOpenDelegate? public var animationVelocity: Double = 0.2 public var sliderViewTopDistance: CGFloat = 8.0 { didSet { topSliderConstraint?.constant = sliderViewTopDistance layoutIfNeeded() } } public var thumbnailViewTopDistance: CGFloat = 0.0 { didSet { topThumbnailViewConstraint?.constant = thumbnailViewTopDistance layoutIfNeeded() } } public var thumbnailViewStartingDistance: CGFloat = 0.0 { didSet { leadingThumbnailViewConstraint?.constant = thumbnailViewStartingDistance trailingDraggedViewConstraint?.constant = thumbnailViewStartingDistance setNeedsLayout() } } public var textLabelLeadingDistance: CGFloat = 100 { didSet { leadingTextLabelConstraint?.constant = textLabelLeadingDistance setNeedsLayout() } }

public var isEnabled:Bool = true {
    didSet {
        animationChangedEnabledBlock?(isEnabled)
    }
}
public var showSliderText:Bool = false {
    didSet {
        sliderTextLabel.isHidden = true //!showSliderText
    }
}
public var animationChangedEnabledBlock:((Bool) -> Void)?
// MARK: Default styles
public var sliderCornerRadius: CGFloat = 30.0 {
    didSet {
        sliderHolderView.layer.cornerRadius = sliderCornerRadius
        draggedView.layer.cornerRadius = sliderCornerRadius
    }
}
public var sliderBackgroundColor: UIColor = UIColor(red:0.1, green:0.61, blue:0.84, alpha:0.1) {
    didSet {
        sliderHolderView.backgroundColor = sliderBackgroundColor
        sliderTextLabel.textColor = UIColor.white
    }
}

public var textColor:UIColor = UIColor(red:25.0/255, green:155.0/255, blue:215.0/255, alpha:0.7) {
    didSet {
        textLabel.textColor = textColor

    }
}

public var slidingColor:UIColor = UIColor(red:25.0/255, green:155.0/255, blue:215.0/255, alpha:0.7) {
    didSet {
        draggedView.backgroundColor = slidingColor
    }
}
public var thumbnailColor:UIColor = UIColor(red:25.0/255, green:155.0/255, blue:215.0/255, alpha:1) {
    didSet {
        thumnailImageView.backgroundColor = thumbnailColor
    }
}
public var labelText: String = "slide to send Red Alert" {
    didSet {
        textLabel.text = labelText
    }
}

public var sliderlabelText: String = "slide to Close Alert" {
    didSet {
        sliderTextLabel.text = sliderlabelText
    }
}
public var textFont: UIFont = UIFont.boldSystemFont(ofSize: 22.0) {
    didSet {
        textLabel.font = textFont
        sliderTextLabel.font = textFont
    }
}
// MARK: Private Properties
private var leadingThumbnailViewConstraint: NSLayoutConstraint?
private var leadingTextLabelConstraint: NSLayoutConstraint?
private var topSliderConstraint: NSLayoutConstraint?
private var topThumbnailViewConstraint: NSLayoutConstraint?
private var trailingDraggedViewConstraint: NSLayoutConstraint?
private var xPositionInThumbnailView: CGFloat = 0
private var xEndingPoint: CGFloat {
    get {
        return (self.view.frame.maxX - thumnailImageView.bounds.width - thumbnailViewStartingDistance)
    }
}
private var isFinished: Bool = false

override public init(frame: CGRect) {
    super.init(frame: frame)
    setupView()
}
private var panGestureRecognizer: UIPanGestureRecognizer!

required public init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)!
    setupView()
}

private func setupView() {
    self.addSubview(view)
    view.addSubview(thumnailImageView)
    view.addSubview(sliderHolderView)
    view.addSubview(draggedView)
    draggedView.addSubview(sliderTextLabel)
    sliderHolderView.addSubview(textLabel)
    view.bringSubview(toFront: self.thumnailImageView)
    setupConstraint()
    setStyle()
    // Add pan gesture
    panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(self.handlePanGesture(_:)))
    panGestureRecognizer.minimumNumberOfTouches = 1
    thumnailImageView.addGestureRecognizer(panGestureRecognizer)
}

private func setupConstraint() {
    view.translatesAutoresizingMaskIntoConstraints = false
    thumnailImageView.translatesAutoresizingMaskIntoConstraints = false
    sliderHolderView.translatesAutoresizingMaskIntoConstraints = false
    textLabel.translatesAutoresizingMaskIntoConstraints = false
    sliderTextLabel.translatesAutoresizingMaskIntoConstraints = false
    draggedView.translatesAutoresizingMaskIntoConstraints = false
    // Setup for view
    view.leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive = true
    view.trailingAnchor.constraint(equalTo: self.trailingAnchor).isActive = true
    view.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
    view.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true

    // Setup for circle View
    leadingThumbnailViewConstraint = thumnailImageView.leadingAnchor.constraint(equalTo: view.leadingAnchor)
    leadingThumbnailViewConstraint?.isActive = true
    topThumbnailViewConstraint = thumnailImageView.topAnchor.constraint(equalTo: view.topAnchor, constant: thumbnailViewTopDistance)
    topThumbnailViewConstraint?.isActive = true
    thumnailImageView.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: CGFloat(0)).isActive = true
    thumnailImageView.heightAnchor.constraint(equalTo: thumnailImageView.widthAnchor).isActive = true

    // Setup for slider holder view
    topSliderConstraint = sliderHolderView.topAnchor.constraint(equalTo: view.topAnchor, constant: sliderViewTopDistance)
    topSliderConstraint?.isActive = true
    sliderHolderView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
    sliderHolderView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
    sliderHolderView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true

    // Setup for textLabel
    textLabel.topAnchor.constraint(equalTo: sliderHolderView.topAnchor).isActive = true
    textLabel.centerYAnchor.constraint(equalTo: sliderHolderView.centerYAnchor, constant: CGFloat(0)).isActive = true
    leadingTextLabelConstraint = textLabel.leadingAnchor.constraint(equalTo: sliderHolderView.leadingAnchor, constant: textLabelLeadingDistance)
    leadingTextLabelConstraint?.isActive = true
    textLabel.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: CGFloat(-8)).isActive = true

    // Setup for sliderTextLabel
    sliderTextLabel.topAnchor.constraint(equalTo: textLabel.topAnchor, constant: CGFloat(0)).isActive = true
    sliderTextLabel.centerYAnchor.constraint(equalTo: textLabel.centerYAnchor, constant: CGFloat(0)).isActive = true
    sliderTextLabel.leadingAnchor.constraint(equalTo: textLabel.leadingAnchor, constant: CGFloat(-105)).isActive = true//-75
    sliderTextLabel.trailingAnchor.constraint(equalTo: textLabel.trailingAnchor,constant: CGFloat(-65)).isActive = true

    // Setup for Dragged View
    draggedView.leadingAnchor.constraint(equalTo: sliderHolderView.leadingAnchor).isActive = true
    draggedView.topAnchor.constraint(equalTo: sliderHolderView.topAnchor).isActive = true
    draggedView.centerYAnchor.constraint(equalTo: sliderHolderView.centerYAnchor, constant: CGFloat(-0)).isActive = true
    trailingDraggedViewConstraint = draggedView.trailingAnchor.constraint(equalTo: thumnailImageView.trailingAnchor, constant: thumbnailViewStartingDistance)
    trailingDraggedViewConstraint?.isActive = true
}

private func setStyle() {
    thumnailImageView.backgroundColor = thumbnailColor
    textLabel.text = labelText
    textLabel.font = textFont
    textLabel.textColor = textColor
    textLabel.textAlignment = .center

    sliderTextLabel.text = sliderlabelText
    sliderTextLabel.font = textFont
    sliderTextLabel.textColor = sliderBackgroundColor
    sliderTextLabel.textAlignment = .center
    sliderTextLabel.isHidden = false//!showSliderText

    sliderHolderView.backgroundColor = sliderBackgroundColor
    sliderHolderView.layer.cornerRadius = sliderCornerRadius
    draggedView.backgroundColor =  slidingColor
    draggedView.layer.cornerRadius = sliderCornerRadius
    draggedView.clipsToBounds = true
    draggedView.layer.masksToBounds = true
}

private func isTapOnThumbnailViewWithPoint(_ point: CGPoint) -> Bool{
    return self.thumnailImageView.frame.contains(point)
}

private func updateThumbnailXPosition(_ x: CGFloat) {
    leadingThumbnailViewConstraint?.constant = x
    setNeedsLayout()
}

// MARK: UIPanGestureRecognizer
@objc private func handlePanGesture(_ sender: UIPanGestureRecognizer) {
    if isFinished || !isEnabled {
        delegate?.mtSlideToCloseDelegateDidFinish(self)
    }

    let translatedPoint = sender.translation(in: view).x
    switch sender.state {
    case .began:
        break
    case .changed:
        if translatedPoint >= xEndingPoint {
            updateThumbnailXPosition(xEndingPoint)

            return
        }
        if translatedPoint <= thumbnailViewStartingDistance {
            textLabel.alpha = 1
            updateThumbnailXPosition(thumbnailViewStartingDistance)
            return
        }
        updateThumbnailXPosition(translatedPoint)
        sliderTextLabel.isHidden = false
        textLabel.alpha = (xEndingPoint - translatedPoint) / xEndingPoint
        break
    case .ended:
        if translatedPoint >= xEndingPoint {
            textLabel.alpha = 0
            updateThumbnailXPosition(xEndingPoint)
            // Finish action
            isFinished = true
            self.thumnailImageView.image = #imageLiteral(resourceName: "brand_ic_notif")
            delegate?.mtSlideToOpenDelegateDidFinish(self)
            return
        }
        if translatedPoint <= thumbnailViewStartingDistance {
            textLabel.alpha = 1
            updateThumbnailXPosition(thumbnailViewStartingDistance)
            return
        }
        UIView.animate(withDuration: animationVelocity) {
            self.leadingThumbnailViewConstraint?.constant = self.thumbnailViewStartingDistance
            self.textLabel.alpha = 1
            self.layoutIfNeeded()
        }
        break
    default:
        break
    }
}
// Others
public func resetStateWithAnimation(_ animated: Bool) {
    let action = {
        self.thumnailImageView.image = #imageLiteral(resourceName: "checkmark")
        self.leadingThumbnailViewConstraint?.constant = self.thumbnailViewStartingDistance
        self.textLabel.alpha = 1
        self.layoutIfNeeded()
        self.isFinished = false
        self.sliderTextLabel.isHidden = true
    }
    if animated {
        UIView.animate(withDuration: animationVelocity) {
            action()
        }
    } else {
        action()
    }
}

}

class MTRoundImageView: UIImageView { override func layoutSubviews() { super.layoutSubviews() let radius: CGFloat = self.bounds.size.width / 2.0 self.layer.cornerRadius = radius } }

ghost commented 4 years ago

Please this delegate method functionality:

if isFinished || !isEnabled { delegate?.mtSlideToCloseDelegateDidFinish(self) }

ghost commented 4 years ago

@objc public protocol MTSlideToOpenDelegate { func mtSlideToOpenDelegateDidFinish( sender: MTSlideToOpenView) func mtSlideToCloseDelegateDidFinish( sender: MTSlideToOpenView) }

balabon7 commented 4 years ago

Hi. Please tell me how to cancel unlocking by swiping to the left when the slider is at the right point? In the swiftUI version. "Same as SharmaVinay, how we can go back with same like swipe to lock again ?" (not for UIKit)

vishalmiOSdev commented 5 months ago

Here we have implemented similar swipe left to right and right to left https://github.com/vishalmiOSdev/VSlideView.git