0
내가 사용하는 UIView에 대한 스펙을 작성하고 싶습니다. UIView는 특정 위치로 드래그하여 Wright 대리자 메서드를 호출하도록 테스트해야합니다. "끌기"방법을 사용하고 있습니다. 그러나 드래그 명령은 드래그하는 것으로 보이지 않습니다. 아마도 나는 그것을 잘못된 방식으로 사용하고 있는가? 영감을 얻으려고 macbacon spec을 사용했습니다. 여기 Rubymotion에서 UIView의 올바른 드래그 테스트하기
코드입니다 :describe "DraggableView" do
tests SpecDragViewController
before do
@drag = MyDragableView.alloc.initWithFrame [[10,200],[100,100]]
@drag.backgroundColor = UIColor.redColor
controller.view.addSubview @drag
end
it "should not accept wrong drop" do
@drag.frame.should != nil
@drag.accessibilityLabel = "Drag View"
delegate = Object.new
delegate.mock!(:draggableViewHasBeenDroppedAtWrongLocation) {|view|
view.should == @drag
}
p @drag.center
drag "Drag View", :from => CGPointMake(15,250), :to => CGPointMake(300,300)
wait 10{} # Long wait, just so I could play with the view in the simulator
p @drag.frame
end
end
class SpecDragViewController < UIViewController
end
class MyDragableView < UIView
attr_accessor :destination_view
attr_accessor :delegate
def initWithFrame (rect)
if super
addBehaviour
end
self
end
private
def addBehaviour
@panGesture = UIPanGestureRecognizer.alloc.initWithTarget(self, action:'dragGesture:');
self.addGestureRecognizer(@panGesture)
self.userInteractionEnabled=true
end
def dragGesture(panGesture)
translation = panGesture.translationInView(self.superview)
case panGesture.state
when UIGestureRecognizerStateBegan
@originalCenter = self.center
delegate.draggableViewStartedDragging(self) if delegate
when UIGestureRecognizerStateChanged
self.center = CGPointMake(self.center.x + translation.x,
self.center.y + translation.y)
when UIGestureRecognizerStateEnded
if (@destination_view && CGRectContainsPoint(@destination_view.frame, self.center) && delegate.draggableViewCanBeDropped?(self))
self.removeGestureRecognizer(@panGesture)
delegate.draggableViewHasBeenDropped(self)
else
delegate.draggableViewHasBeenDroppedAtWrongLocation(self)
end
end
panGesture.setTranslation(CGPointZero, inView:self.superview)
end
end