Skip to content

Commit

Permalink
fix(TrimmerView): Fixed frames
Browse files Browse the repository at this point in the history
Fixed TrimmerVIew issue when only first frame being shown

closes out #33
  • Loading branch information
Shahen Hovhannisyan committed Mar 1, 2017
1 parent 1528103 commit f1eca46
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 176 deletions.
Binary file added ios/.DS_Store
Binary file not shown.
Binary file added ios/RNVideoProcessing/.DS_Store
Binary file not shown.
Binary file added ios/RNVideoProcessing/RNTrimmerView/.DS_Store
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ - (CGFloat)thumbWidth
- (void)resetSubviews
{
self.clipsToBounds = YES;

[self setBackgroundColor:[UIColor blackColor]];

[self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame))];
Expand Down Expand Up @@ -141,7 +141,7 @@ - (void)resetSubviews

// width for left and right overlay views
self.overlayWidth = CGRectGetWidth(self.frame) - (self.minLength * self.widthPerSecond);

// add left overlay view
self.leftOverlayView = [[UIView alloc] initWithFrame:CGRectMake(self.thumbWidth - self.overlayWidth, 0, self.overlayWidth, CGRectGetHeight(self.frameView.frame))];
CGRect leftThumbFrame = CGRectMake(self.overlayWidth-self.thumbWidth, 0, self.thumbWidth, CGRectGetHeight(self.frameView.frame));
Expand All @@ -156,15 +156,15 @@ - (void)resetSubviews
self.trackerView.layer.masksToBounds = true;
self.trackerView.layer.cornerRadius = 2;
[self addSubview:self.trackerView];

[self.leftThumbView.layer setMasksToBounds:YES];
[self.leftOverlayView addSubview:self.leftThumbView];
[self.leftOverlayView setUserInteractionEnabled:YES];
UIPanGestureRecognizer *leftPanGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(moveLeftOverlayView:)];
[self.leftOverlayView addGestureRecognizer:leftPanGestureRecognizer];
[self.leftOverlayView setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.8]];
[self addSubview:self.leftOverlayView];

// add right overlay view
CGFloat rightViewFrameX = CGRectGetWidth(self.frameView.frame) < CGRectGetWidth(self.frame) ? CGRectGetMaxX(self.frameView.frame) : CGRectGetWidth(self.frame) - self.thumbWidth;
self.rightOverlayView = [[UIView alloc] initWithFrame:CGRectMake(rightViewFrameX, 0, self.overlayWidth, CGRectGetHeight(self.frameView.frame))];
Expand Down Expand Up @@ -267,7 +267,7 @@ - (void)moveRightOverlayView:(UIPanGestureRecognizer *)gesture
}

- (void)seekToTime:(CGFloat) time
{
{
CGFloat posToMove = time * self.widthPerSecond + self.thumbWidth - self.scrollView.contentOffset.x;

CGRect trackerFrame = self.trackerView.frame;
Expand Down Expand Up @@ -360,13 +360,8 @@ - (void)addFrames
}
tmp.frame = currentFrame;

dispatch_async(dispatch_get_main_queue(), ^{
[self.frameView addSubview:tmp];
});


[self.frameView addSubview:tmp];
}

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
for (int i=1; i<=[times count]; i++) {
CMTime time = [((NSValue *)[times objectAtIndex:i-1]) CMTimeValue];
Expand Down
328 changes: 164 additions & 164 deletions ios/RNVideoProcessing/RNTrimmerView/RNTrimmerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,169 +8,169 @@ import AVKit

@objc(RNTrimmerView)
class RNTrimmerView: RCTView, ICGVideoTrimmerDelegate {

var trimmerView: ICGVideoTrimmerView?
var asset: AVAsset!
var rect: CGRect = CGRect.zero
var mThemeColor = UIColor.clear
var bridge: RCTBridge!
var onChange: RCTBubblingEventBlock?
var _minLength: CGFloat? = nil
var _maxLength: CGFloat? = nil
var _trackerColor: UIColor = UIColor.clear

var source: NSString? {
set {
setSource(source: newValue)
}
get {

return nil
}
}

var height: NSNumber? {
set {
self.rect.size.height = RCTConvert.cgFloat(newValue)
self.updateView()
}
get {
return nil
}
}

var width: NSNumber? {
set {
self.rect.size.width = RCTConvert.cgFloat(newValue)
self.updateView()
}
get {
return nil
}
}

var themeColor: NSString? {
set {
if newValue != nil {
let color = NumberFormatter().number(from: newValue! as String)
self.mThemeColor = RCTConvert.uiColor(color)
self.updateView()
}
}
get {
return nil
}
}

var maxLength: NSNumber? {
set {
if newValue != nil {
self._maxLength = RCTConvert.cgFloat(newValue!)
self.updateView()
}
}
get {
return nil
}
}

var minLength: NSNumber? {
set {
if newValue != nil {
self._minLength = RCTConvert.cgFloat(newValue!)
self.updateView()
}

var trimmerView: ICGVideoTrimmerView?
var asset: AVAsset!
var rect: CGRect = CGRect.zero
var mThemeColor = UIColor.clear
var bridge: RCTBridge!
var onChange: RCTBubblingEventBlock?
var _minLength: CGFloat? = nil
var _maxLength: CGFloat? = nil
var _trackerColor: UIColor = UIColor.clear

var source: NSString? {
set {
setSource(source: newValue)
}
get {

return nil
}
}

var height: NSNumber? {
set {
self.rect.size.height = RCTConvert.cgFloat(newValue)
self.updateView()
}
get {
return nil
}
}

var width: NSNumber? {
set {
self.rect.size.width = RCTConvert.cgFloat(newValue)
self.updateView()
}
get {
return nil
}
}

var themeColor: NSString? {
set {
if newValue != nil {
let color = NumberFormatter().number(from: newValue! as String)
self.mThemeColor = RCTConvert.uiColor(color)
self.updateView()
}
}
get {
return nil
}
}

var maxLength: NSNumber? {
set {
if newValue != nil {
self._maxLength = RCTConvert.cgFloat(newValue!)
self.updateView()
}
}
get {
return nil
}
}

var minLength: NSNumber? {
set {
if newValue != nil {
self._minLength = RCTConvert.cgFloat(newValue!)
self.updateView()
}
}
get {
return nil
}
}

var currentTime: NSNumber? {
set {
print("CHANGED: [TrimmerView]: currentTime: \(newValue)")
if newValue != nil && self.trimmerView != nil {
let convertedValue = newValue as! CGFloat
self.trimmerView?.seek(toTime: convertedValue)
// self.trimmerView
}
}
get {
return nil
}
}

var trackerColor: NSString? {
set {
if newValue == nil {
return
}
if self.trimmerView == nil {
return
}
let color = NumberFormatter().number(from: newValue! as String)
let formattedColor = RCTConvert.uiColor(color)
if formattedColor != nil {
self._trackerColor = formattedColor!
self.updateView()
}
}
get {
return nil
}
}

func updateView() {
self.frame = rect
if trimmerView != nil {
trimmerView!.frame = rect
trimmerView!.themeColor = self.mThemeColor
trimmerView!.trackerColor = self._trackerColor
trimmerView!.maxLength = _maxLength == nil ? CGFloat(self.asset.duration.seconds) : _maxLength!
if _minLength != nil {
trimmerView!.minLength = _minLength!
}
self.trimmerView!.resetSubviews()
// Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(self.updateTrimmer), userInfo: nil, repeats: false)
}
}

func updateTrimmer() {
self.trimmerView!.resetSubviews()
}

func setSource(source: NSString?) {
if source != nil {
let pathToSource = NSURL(string: source! as String)
self.asset = AVURLAsset(url: pathToSource as! URL, options: nil)

trimmerView = ICGVideoTrimmerView(frame: rect, asset: self.asset)
trimmerView!.showsRulerView = false
trimmerView!.hideTracker(false)
trimmerView!.delegate = self
trimmerView!.trackerColor = self._trackerColor
self.addSubview(trimmerView!)
self.updateView()
}
}

init(frame: CGRect, bridge: RCTBridge) {
super.init(frame: frame)
self.bridge = bridge
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

func onTrimmerPositionChange(startTime: CGFloat, endTime: CGFloat) {
if self.onChange != nil {
let event = ["startTime": startTime, "endTime": endTime]
self.onChange!(event)
}
}

func trimmerView(_ trimmerView: ICGVideoTrimmerView, didChangeLeftPosition startTime: CGFloat, rightPosition endTime: CGFloat) {
onTrimmerPositionChange(startTime: startTime, endTime: endTime)
}
get {
return nil
}
}

var currentTime: NSNumber? {
set {
print("CHANGED: [TrimmerView]: currentTime: \(newValue)")
if newValue != nil && self.trimmerView != nil {
let convertedValue = newValue as! CGFloat
self.trimmerView?.seek(toTime: convertedValue)
// self.trimmerView
}
}
get {
return nil
}
}

var trackerColor: NSString? {
set {
if newValue == nil {
return
}
if self.trimmerView == nil {
return
}
let color = NumberFormatter().number(from: newValue! as String)
let formattedColor = RCTConvert.uiColor(color)
if formattedColor != nil {
self._trackerColor = formattedColor!
self.updateView()
}
}
get {
return nil
}
}

func updateView() {
self.frame = rect
if trimmerView != nil {
trimmerView!.frame = rect
trimmerView!.themeColor = self.mThemeColor
trimmerView!.trackerColor = self._trackerColor
trimmerView!.maxLength = _maxLength == nil ? CGFloat(self.asset.duration.seconds) : _maxLength!
if _minLength != nil {
trimmerView!.minLength = _minLength!
}
self.trimmerView!.resetSubviews()
Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(self.updateTrimmer), userInfo: nil, repeats: false)
}
}

func updateTrimmer() {
self.trimmerView!.resetSubviews()
}

func setSource(source: NSString?) {
if source != nil {
let pathToSource = NSURL(string: source! as String)
self.asset = AVURLAsset(url: pathToSource as! URL, options: nil)

trimmerView = ICGVideoTrimmerView(frame: rect, asset: self.asset)
trimmerView!.showsRulerView = false
trimmerView!.hideTracker(false)
trimmerView!.delegate = self
trimmerView!.trackerColor = self._trackerColor
self.addSubview(trimmerView!)
self.updateView()
}
}

init(frame: CGRect, bridge: RCTBridge) {
super.init(frame: frame)
self.bridge = bridge
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

func onTrimmerPositionChange(startTime: CGFloat, endTime: CGFloat) {
if self.onChange != nil {
let event = ["startTime": startTime, "endTime": endTime]
self.onChange!(event)
}
}

func trimmerView(_ trimmerView: ICGVideoTrimmerView, didChangeLeftPosition startTime: CGFloat, rightPosition endTime: CGFloat) {
onTrimmerPositionChange(startTime: startTime, endTime: endTime)
}
}

0 comments on commit f1eca46

Please sign in to comment.